Thursday, December 29, 2011

PHP Shortcuts of If-Else statement

One day i was assigned to update a website which created by my mentor in php, while updating the some parts of the function I've found a 1 line of which I've never encountered before in my PHP life, so i asked him if what is the purpose of this line of code then he said it is a shortcut If statement in php, i placed the sample line of codes below, please check.

$authenticated = true; 
// 1 standard if-else statement 
if ($authenticated) 
{ 
 echo 'YES'; 
} 
else 
{       
 echo 'NO'; 
} 
// same with 1 
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';



No comments:

Post a Comment

 

blogger templates | Make Money Online