Two ways to write php ternary operator

怪我咯
Release: 2023-03-10 21:42:01
Original
2653 people have browsed it

This article mainly introduces two code examples of PHPternary operation. Friends who need it can refer to it

First, let’s look at one now Simple example:

The code is as follows:

<?php
$a = 2;
($a == 1) ? $test = "企业" : $test = "地区";#写法一
echo
 $test;
?>
Copy after login

As for the above example, first determine whether $a is 1. If the string of "enterprise" is saved Enter the $test variable and then output it. If the string "region" is not stored in the $test variable and then output it;
The appeal example code is equivalent to:

The code is as follows:

<?php
$a = 2;

#写法二
$test = ($a == 1) ? "企业" : "地区";

#写法三
if($a == 1){
   $test="企业";
}else{
   $test="地区";
}
echo $test;

?>
Copy after login

The above is the detailed content of Two ways to write php ternary operator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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