Usage of ternary operators in php

Source: Internet
Author: User

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

Syntax: condition? Result 1: result 2

Note: the position before the question mark is the judgment condition. If the condition is met, result 1 is displayed. If the condition is not met, result 2 is displayed.

 

The Code is as follows: Copy code

<? 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 whether $ _ 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 of $ id depends on whether $ _ GET ['id'] exists. Therefore, if $ _ GET ['id'] exists, $ id = $ _ GET ['id']; otherwise, $ id = false.


Example

Use "? : "Condition statement to test user input values:

The Code is as follows: Copy code

<? 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 code using the ternary operator is equivalent to the following code:

The Code is as follows: Copy code

<? Php
If (isset ($ argv [1]) {
$ Filename = $ argv [1];
} Else {
$ Filename = "php: // stdin ";
}
?>

It can be seen that if the above Code is written in a normal if-else structure, the amount of code will be much larger than above, but the second form is easier to understand and does not require more input. Therefore, when selecting a ternary operator, you must weigh the advantages and disadvantages.

Advantages of ternary Operators

The ternary operators (? :) It greatly reduces the time for programmers to write these statements. Its syntax is as follows:

Condition? Do_if_true: do_if_false;

Ternary operators are not an essential structure, but they are a way to beautify the code. Similarly, it can replace bad if... Else code block, which can improve the readability of the Code.

Similarly, you can use the PHP or computing server to assign the default value of the variable:

The Code is as follows: Copy code

<? 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.