Introduction to the use of php ternary operators, php operator _ PHP Tutorial

Source: Internet
Author: User
Tags php operator
Introduction to the use of php ternary operators, php operators. Introduction to the use of php ternary operators. when we write php operators, if {...} else {...} most of them are used, but sometimes we can use the ternary operation in C to introduce the use of the php ternary operator and the php operator.

When we write PHP, it is possible that if {...} else {...} most of them are used, but sometimes we can use the ternary operation in C to reduce the code! This article describes some tips and notes for using ternary computation in php development. For more information about the coders, see.

Today, a netizen posted a question in the group, but it may be wrong.

echo $a == 1 ? 'one' : $a == 2 ? 'two' : $a == 3 ? 'three' : $a == 4 ? 'foura' : 'other'; echo "\n"; 

The output result is:




Result: four

I cannot understand it at first. according to my understanding, the logic is as follows:
Echo ($ a = 1? 'One ':
($ A = 2? 'Two ':
($ A = 3? 'Three ':
($ A = 4? 'Four': 'Other '))));
Output: two

Later, with the help of kevinG (qq: 48474), I read the php manual and finally understood that the interpretation of the php trielement was from left to right,

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.