How to use php ternary operator

王林
Release: 2023-02-24 14:10:02
Original
8661 people have browsed it

How to use php ternary operator

1. Ternary operator format: (expr1) ? (expr2) : (expr3)

2. Logical operation

How to use php ternary operator

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;
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template