With the release of PHP 5.3.0 there is an option to shorten the ternary operator.
So, if we start with variable $foo, which is true ($foo = true), instead of writing:
$bar = $foo ? true : false; // $bar = true
you can easily shorten it like this
$bar = $foo ?: false // $bar = true
The bad side of using a short ternary operator is that most of PHP IDEs are marking it as syntax error (well, at least for now)
Method chaining is a technique that adds class methods one on top the other. It’s commonly used in jQuery and models in PHP frameworks.
The whole “magic” behind object chaining is made with a single line – “return $this” which is added in each method you wish to chain.
So, in order to get this behavior in PHP:
$human = new Human;
$human->setName('Haris')
->setAge(30)
->setSex('male')
->display();
you have to define class(es) like this:
class Human {
protected $_name;
protected $_age;
protected $_sex;
public function setName($name) {
$this->_name = $name;
return $this;
}
public function setAge($age) {
$this->_age = $age;
return $this;
}
public function setSex($sex) {
$this->_sex = $sex;
return $this;
}
public function display() {
printf('Hi, I\'m %s. I\'m %d years old, and I\'m %s!',
$this->_name,
$this->_age,
$this->_sex
);
}
}
class Man extends Human {
protected $_sex = 'male';
}
class Woman extends Human {
protected $_sex = 'female';
}
Now compare the methods called with method chaining and the old fashioned way
$woman = new Woman;
$woman->setName('Jane')
->setAge(27)
->display();
$woman = new Woman;
$woman->setName('Jane');
$woman->setAge(27);
$woman->display();
// Hi, I'm Jane. I'm 27 years old, and I'm female!
I was reading regular expressions docs to find a solution for a problem that I had. And like all the greatest discoveries (that are found accidentally), I found this neath little “escaping” trick.
The big idea behind it is to grab the matching elements in patterns section and create an escape sequence with PHP code in the replacement part.
Example
$txt = 'This is a test';
print preg_replace("/(\>)([\w\s]+)(\<)/e", "'\\1'.strtoupper('\\2').'\\3'", $txt);
Pattern explanation
(\>) //matches the greater than sign of the opening div element and ads this element to a first subpattern
([\w\s]+) //second subpattern will grab all word characters and any white space
(\<) //third subpattern matches the less than sign of the closing div tag
Replacement explanation
‘\\1′ is used to access a first subpattern, ‘\\2′ for accessing second etc… These subpatterns are naturally available as strings. Since PHP uses a dot (.) as a string concatenation operator, we add dots before and after PHP code, in this example strtoupper(‘\\2′)
To a “strtoupper” function (which converts all characters to uppercase) we pass a second subpattern, and we close the replacement part with a third.
So, what we are doing here is actually matching any text between the div tags and convert that text to uppercase letters.
The end result of this small example would be THIS IS A TEST

I read about this interesting challenge at http://f055.net/article/the-140-character-webapp-challenge/. The idea was to create a web application in 140 characters or less (one twitter message).
I was instantly hooked.
Most of the guys submitted variations of a micro twitter, so naturally, I decided to go for something else. I figured out it would be great to write an application that parses RSS/RDF feeds :)
So, after more then an hour I ended up writing this 135 characters web app:
<?php foreach(file($_GET[u])as$l)if(preg_match('/<(title|link)>([^<]+)/',$l,$m))echo$m[1]=='link'?" <a href=$m[2]>link</a><hr>":$m[2]?>
You can check out live demo here.
I am really glad that I managed to format the output the way I wanted, to have article links and separators, and not only to dump data to browser.
Coding this little app was pure fun. It really keeps you going when you try so squeeze so much stuff into only <= 140 chars. Any programmer out there should try to write one of these small apps.