Funny, i've just tested something with a fellow coder.
It seems using ternary operators in PHP is slower then binary if {} else {}
PHP Code:
<?php
$time = microtime(true);
for ($i=0; $i<1000000; ++$i)
{
$foo = ($i%2==0) ? 0 : 1;
}
$time = microtime(true)-$time;
echo 'ternary in: '.round($time,4).' seconds<br>';
$time = microtime(true);
for ($i=0; $i<1000000; ++$i)
{
if ($i%2==0)
{
$foo = 0;
}
else
{
$foo = 1;
}
}
$time = microtime(true)-$time;
echo 'if..else.. in: '.round($time,4).' seconds<br>';
Result P4 2.54
Code:
ternary in: 0.5543 seconds
if..else.. in: 0.5709 seconds
Result P2
Code:
ternary in: 3.4309 seconds
if..else.. in: 2.7495 seconds
It's hardly noticeable though since how often do you use the ternary?