Wednesday 25 July 2018

Ternary Operator in PHP

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:


$a = ($b>5) ? 3 : 5;

Thus, we have seen how much lines of codes is reduced.

No comments:

Post a Comment

Parenting tips to inculcate learning habits in your kid

Parenting tips to inculcate learning habits in your kid Tip #1) Children do not learn things, they emitate. So, try to do things by yours...