View Single Post
Old 06-10-2008, 04:26 PM   #5 (permalink)
sketchMedia
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Salathe has made use of the ternary operator, it works like this:

( expression) ? ( expression2 ) : (expression3)

If expression is true, then PHP will evaluate to ( expression2 )
If expression1 is false, then it evaluates to ( expression3 )

To help you understand it further it can be rewritten like so:

PHP Code:
if(($pos strpos($text0'(')) !== FALSE ){
    
$text2 substr($text00$pos);
}else{
    
$text2 FALSE;

So, first PHP creates '$pos' and assigns the result of 'strpos' to it, then it functions like a regular IF, so if $pos is not equal to FALSE, or $pos not of the same type (which is what the !== comparison operator means, this is used because it will return an int on success and a bool false if it failed) then assign result of substr to $text2, else assign it FALSE.

As you can see using the ternary is smaller but at the same time can be tricky to read, especially if you nest them, which you shouldnt do, not unless you want to give yourself a headache, look at this snippet posted by salathe in another post:
PHP Code:
echo chr(16 + ((strlen('talkphp') + 1337) >> ((true === false
     ? ((
'a' === chr(0x61)) ? 'talkphp' 'php'
     : ((
767 & (405 - (pow(12* ('0' 
     
false true))))))))), 
     
chr('happy' 0x29 0x28); 
edit : damn delayedinsanity beat me to it, i must learn to type faster!
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following 2 Users Say Thank You to sketchMedia For This Useful Post:
Matt (06-10-2008), pipesportugal (06-11-2008)