Returns one of two expressions depending on a condition.
Syntax
expression ? expression1 : expression2
Returns expression1 if the 'expression' evaluates to true other wise returns expression 2.
Example
$msg1 = ($a % 2 == 0) ? "Even number" : "Odd number";
ternary_operator_demo.php
#!/usr/bin/php
<?php
$a = 10;
$b = 11;
$msg1 = ($a % 2 == 0) ? "Even number" : "Odd number";
$msg2 = ($b % 2 == 0) ? "Even number" : "Odd number";
echo "$a is $msg1\n";
echo "$b is $msg2";
?>
Output
$./ternary_operator_demo.php
10 is Even number
11 is Odd number
Previous Next Home
No comments:
Post a Comment