Home >Backend Development >PHP Tutorial >Two ways to write php ternary operator

Two ways to write php ternary operator

怪我咯
怪我咯Original
2017-06-20 14:00:272673browse

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!

Statement:
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