Re: a shortcut to set variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux