Ternary operator usage in PHP

Source: Internet
Author: User
Tags stdin

Syntax: Condition? Result 1: Results 2

Description: The position of the front of the question mark is the condition of judgment, if the condition is satisfied with the result 1, the result is not satisfied 2.

The code is as follows

? Php

$id = Isset ($_get[' id ')? $_get[' id ': false;

?>

A piece of code replaces a lot of code. First, it uses the Isset () function to check if the $_get[' ID ' exists. If $_get[' id ' does exist, it returns its value. However, if it does not exist, the condition is false, and false is returned. The value $id depends on whether the $_get[' id ' exists. So, basically, if the $_get[' id ' exists, $id =$_get[' id '], and vice versa $id=false.


Cases

Validate user input values with the "?:" Conditional statement:

The code is as follows

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

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

The code is as follows

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

It can be seen that, assuming the normal if-else structure to write the above code, the amount of code will be more than the above, but the second form is easier to understand, and does not require more input. So when choosing ternary operators, make sure you weigh the pros and cons.

Advantages of ternary operators

The ternary operator (?:) in PHP greatly reduces the time that programmers write these statements. Its syntax is as follows:

Condition? Do_if_true:do_if_false;

The ternary operator is not an essential structure, but it is a way to beautify the code. Again, it can replace bad If...else code blocks and improve the readability of your code.

Similarly, a user can use the PHP or operator service to assign a variable default value:

The code is as follows

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.