PHP5.3’s ?: expression
I was digging through the PHP5.3 release info and came across a really neat little expression. It’s a simplified version of the “() ? : ;” expression, a handy little expression to use in variable assignment. The way this expression works is as follows:
In normal PHP syntax, this equates to something like this:
As you can see, the former is much prettier and more concise. Generally, the usage of this expresssion will be when you want to check the existance of a vale, use it if it exists and fall back on a default if it doesn’t.
Using the new syntax in PHP5.3 we can shorten this even more:
Now that’s much better. This unimpressing little expression can clean up your syntax, make your code a lot easier to read and is another reason to upgrade to PHP5.3 as soon as you can.