Ternary operator is a shrtcut operator for if else statement.
Syntax:
$var = (condition) ? if yes, value : if no, value;
For example:
If you have an if else statement like this:
if ($b>5) {
$a = 3;
}
else {
$a = 5;
}
The same thing can be wrote with ternary operator in a single line like:
Syntax:
$var = (condition) ? if yes, value : if no, value;
For example:
If you have an if else statement like this:
if ($b>5) {
$a = 3;
}
else {
$a = 5;
}
The same thing can be wrote with ternary operator in a single line like:
$a = ($b>5) ? 3 : 5;
Thus, we have seen how much lines of codes is reduced.
No comments:
Post a Comment