Re: a shortcut to set variable

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

 



On 12 April 2011 14:25, tedd <tedd.sperling@xxxxxxxxx> 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

-- 
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