Home  >  Article  >  Backend Development  >  Usage of ternary operator in php_PHP tutorial

Usage of ternary operator in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:17:00803browse

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;

?>

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


?>

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.

 代码如下  

$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);
?>

Example

 代码如下  

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

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

The code is as follows
$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);

?>
 代码如下  

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

The previous code using the ternary operator is equivalent to the following code:

The code is as follows
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
$filename = $argv[1] or $filename = "php://stdin";<🎜> ?>
http://www.bkjia.com/PHPjc/372094.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/372094.htmlTechArticleSyntax: Condition? Result 1: Result 2 Description: The position in front of the question mark is the condition for judgment. If the condition is met Result 1, result 2 if not satisfied. The code is as follows ?PHP $id = isset($_GET['id...
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