C3rd
[PHP] Shortcuts of If-Else statement
Posted: 5 Apr 2009, 16:58pm - Sunday
When I learn PHP last September 2005, I was little bit confuse in PHP if-else statement... especially a statement contains with ? and : but somehow, I got it right. I post this cause somebody ask again what's the difference. So told him, its all the same and its just a shortcuts.
<?php
$authenticated = true;
// [1] standard if-else statement
if ($authenticated)
{
echo 'YES';
}
else
{
echo 'NO';
}
// same with [1] :P
if ($authenticated) { echo 'YES'; } else { echo 'NO'; }
// [2] lil bit shortcut
if ($authenticated)
echo 'YES';
else
echo 'NO';
// [3] the shortcut
echo $result = ($authenticated) ? 'YES' : 'NO';
?>
Or let me say its an evolution of if-else statement... Hahaha.. or maybe the laziness of the programmer. :P So got it? hehehehe..