Home >Backend Development >PHP Tutorial >Detailed explanation of the usage of ternary operator in php_PHP tutorial
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
|
Copy code
|
||||||||||||||||
$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. Example Use "?:" conditional statement to check user input value:
@fclose($fp); 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 Previous article:Detailed explanation of PHP file upload_PHP tutorialNext article:Detailed explanation of PHP file upload_PHP tutorial Related articlesSee more |