Use Ternary operation instead of if else

by rajesh 2009-10-29 09:24:29

A normal if else statement would look like
if ($today == "sunday")
{
print "happy life.... ";
} else
{
print "life is toooooo boring?";
}



this checks the condition if $today is sunday and prints 'happy life' if true, else prints 'life is toooooo boring?'

we can make this happen in a single line of code using ternary operator

Syntax - $variable = condition ? if true : if false
$result = ($today == "sunday") ? "happy life.... " : "life is toooooo boring?";
print $result;

this can also be done as
print ($today == "sunday") ? "happy life.... " : "life is toooooo boring?";
// here I dint store the result in a variable. I just printed it..


Tagged in:

1270
like
0
dislike
0
mail
flag

You must LOGIN to add comments