Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of ternary operator in php_PHP tutorial

Detailed explanation of the usage of ternary operator in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:52995browse

The ternary operator is a fixed format in software programming, namely (?:) (Note: The content in brackets is the correct format).

Syntax: Condition ? Result 1 : Result 2

Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2.

The code is as follows
 代码如下 复制代码

$id = isset($_GET['id']) ? $_GET['id'] : false;

?>

Copy code

$id = isset($_GET['id']) ? $_GET['id'] : false;

?>

 代码如下 复制代码

$filename = isset($argv[1]) ? $argv[1] : "php://stdin";
$fp = @fopen($filename, ‘r’) or die("Can’t Open $filename for reading");

while (!@feof($fp)) {
$line = @fgets($fp, 1024);
print $line;
}

@fclose($fp);
?>

 代码如下 复制代码

if (isset($argv[1])) {
$filename = $argv[1];
} else {
$filename = "php://stdin";
}
?>

One code replaces many codes. First, it uses the isset() function to check if $_GET['id'] exists. If $_GET['id'] does exist, it will return its value. However, if it does not exist, the condition is false and false is returned. The value of $id depends on whether $_GET['id'] exists. So, basically, if $_GET['id'] exists, $id=$_GET['id'], otherwise $id=false.

Example

Use "?:" conditional statement to check user input value:

$line = @fgets($fp, 1024); Print $line;
The code is as follows Copy code

$filename = isset($argv[1]) ? $argv[1] : "php://stdin";

$fp = @fopen($filename, ‘r’) or die("Can’t Open $filename for reading");

代码如下 复制代码

$filename = $argv[1] or $filename = "php://stdin";
?>

while (!@feof($fp)) {
}

@fclose($fp);

?> The previous code using the ternary operator is equivalent to the following code:
The code is as follows Copy code
if (isset($argv[1])) { $filename = $argv[1]; } else {

$filename = "php://stdin"; }
?> It can be seen that if the above code is written using an ordinary if-else structure, the amount of code will be much more than the above, but the second form is easier to understand and does not require more input. So when choosing a ternary operator, be sure to weigh the pros and cons. Advantages of ternary operator The ternary operator (?:) in PHP greatly reduces the time programmers spend writing these statements. Its syntax is as follows: condition ? do_if_true : do_if_false; The ternary operator is not an essential construct, but it is a way to beautify the code. Likewise, it can replace bad if...else blocks and improve the readability of your code. Similarly, users can use PHP’s or operator to assign default values ​​to variables:
The code is as follows Copy code
$filename = $argv[1] or $filename = "php://stdin";<🎜> ?> http://www.bkjia.com/PHPjc/628758.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628758.htmlTechArticleThe ternary operator is a fixed format in software programming, that is (?:) (Note: inside the brackets content is the correct format). Syntax: Condition ? Result 1 : Result 2 Description: Ask...
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