|
|
Use Ternary operation instead of if else - PHP
|
Views : 427
|
|
Tagged in : PHP
|
|
|
Report This Scrap as Inappropriate We request you to choose the appropriate categroy and subcategory that suits your
objectionable concern about the scrap, So that our team can review and find out whether it violates our Guidelines or the
scrap is not suitable for all viewers.
|
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..
|
|
By rajesh, On - 2009-10-29 |
|
|
|