Home >Backend Development >PHP Tutorial >Two ways to write php ternary operator
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; ?>
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; ?>
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!