Home >Backend Development >PHP Problem >How to use php ternary operator
1. Ternary operator format: (expr1) ? (expr2) : (expr3)
2. Logical operation
Note: Remember to add parentheses for addition and subtraction operations, otherwise the system may not recognize them sometimes.
Example:
<?php /** * Created by PhpStorm. * User: 洋 汪 * Date: 2016/7/14 * Time: 16:22 */ header("Content-type:text/html;charset=utf-8"); $a = 100; $b = 50; //注意加减法要加括号,不然不识别。 echo "a + b=" . ($a + $b) . "<br>"; if ($a > 10 && $b > 10) { echo "a和b都大于10" . "<br>"; } else { echo "a和b都小于10" . "<br>"; } //三目运算符: $c = 60; //如果$c与50相等,则$v的值为字符串:真值;否则,$v的值为字符串:假值。 $v = ($c == 50) ? "真值" : "假值"; echo $v; ?>
Recommended tutorial: PHP video tutorial
The above is the detailed content of How to use php ternary operator. For more information, please follow other related articles on the PHP Chinese website!