Re: a shortcut to set variable

From: Date: Tue, 12 Apr 2011 13:34:10 +0000
Subject: Re: a shortcut to set variable
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-312354@lists.php.net to get a copy of this message
On 12 April 2011 14:25, tedd <tedd.sperling@gmail.com> wrote: > At 12:47 PM +0100 4/12/11, Richard Quadling wrote: >> >> Putting () around ternary's is a "best practise". >> >> <?php >> echo 'Are we ready to go? ', isset($go) ? 'Yes we are.' : 'No we >> are >> not.', ' Ready state completed', PHP_EOL; >> $go = true; >> echo 'Are we ready to go? ', isset($go) ? 'Yes we are.' : 'No we >> are >> not.', ' Ready state completed', PHP_EOL; >> ?> >> >> vs. >> >> <?php >> echo 'Are we ready to go? ' . isset($go) ? 'Yes we are.' : 'No we >> are >> not.' . ' Ready state completed' . PHP_EOL; >> $go = true; >> echo 'Are we ready to go? ' . isset($go) ? 'Yes we are.' : 'No we >> are >> not.' . ' Ready state completed' . PHP_EOL; >> ?> >> >> vs. >> >> <?php >> echo 'Are we ready to go? ' . (isset($go) ? 'Yes we are.' : 'No we >> are >> not.') . ' Ready state completed' . PHP_EOL; >> $go = true; >> echo 'Are we ready to go? ' . (isset($go) ? 'Yes we are.' : 'No we >> are >> not.') . ' Ready state completed' . PHP_EOL; >> ?> >> >> Don't run them just yet. >> >> Can you quickly and easily see the output? > > Argg!  :-) > > $ready = 'Are we ready to go? '; > $yes = 'Yes we are.'; > $no = 'No we are not.' > > echo($ready); > echo(isset($go) ? $yes : $no); > > > Cheers, > > tedd > > > -- > ------- > http://sperling.com/ > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Tedd, echo is a construct and the () are not required. ;-p The purpose of the code was not to demonstrate any particular coding style, but what happens if you use a ternary without () around it. Without running the code, can you say what the output will be in the 3 examples? -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

« previous php.general (#312354) next »