Table of Contents
php三元运算符详解,运算符详解
Home php教程 php手册 php三元运算符详解,运算符详解

php三元运算符详解,运算符详解

Jun 13, 2016 am 09:03 AM
operator

php三元运算符详解,运算符详解

今天在改论文在线的时候遇到了一个语句看不懂:
$if_summary = $row['IF_SUMMARY']==2?'是':'否';
后来百度后发现是PHP的三元运算符
这句话的意思就等于是
if($row['IF_SUMMARY']==2){
$if_summary="是";
}else{
$if_summary="否";
}


三元运算符的功能与“if...else”流程语句一致,它在一行中书写,代码非常精练、执行效率更高。

在PHP程序中恰当地使用三元运算符能够令脚本更为简洁、高效。

代码格式如下:(expr1) ? (expr2) : (expr3);
解释:如果条件“expr1”成立,则执行语句“expr2”,否则执行“expr3”。
实现同样的功能,若使用条件流程语句,就需要书写多行代码:
if(expr1) {

expr2;
} else {
expr3;
}
可见,前面所说的三元运算符之好并非夸张。可是,多数情况下我们只在代码较为简单的时候使用三元运算符,即执行语句只为单句的时候。如:
$a>$b ? print "a大于b" : print "a小于b";
事实上,三元运算符可以扩展使用,当设置的条件成立或不成立,执行语句都可以不止一句,试看以下格式:
(expr1) ? (expr2).(expr3) : (expr4).(expr5);
我们非常明显地看到,多个执行语句可以使用用字符串运算符号(“.”)连接起来,各执行语句用小角括号包围起来以表明它是一个独立而完整的执行语句。这样扩展后它的功能更为逼近“if...else”流程语句。
同时三元运算符还可以嵌套使用。例如,a大于b成立时:如果a小于c,那么x=c-a否则x=a-c;否则a小于b成立时:如果b小于c,那么x=c-b否则x=b-c:
$a>$b ? $x=($a嵌套使用的三元运算符可读性不太好,日后对代码的维护极可能存在问题,但比起“if...else”之类的流程语句,在上述情形之下,它的确太简练了,这是它的诱人之处。
对于喜欢偷懒和追求代码简洁的人来说,用三元运算符取代if流程语句应该是绝佳的选择。即使不用考虑“三元”中条件句以外的任意某一“元”,使用三元运算符仍然比if语句简练。以下语句的语法是正确的,它们以小解引号的方式忽略了第二或第三“元”:
$a>$b ? print "Yes" : "";
$a>$b ? '': print 'No';
应该注意的是:在使用三元运算符时,建议使用print语句替代echo语句。

注意下列一串语句的理解:

$str = $_GET['abc'] ? 'wangjinbo' : 'wjb';

这里不能理解为:当$str等于$_GET['abc']  时,赋值为 'wangjinbo'  否则赋值为 'wjb';因为一:判断相等应该用==;因原二:三元算符的语法是如上所示:(expr1) ? (expr2) : (expr3),显然上面的二元,三元'wangjinbo'或'wjb'不能单独构成一个有意义的表达式;

正确理解是:当$_GET['abc']为空(也就是否,PHP中 '',null,0,undifine,都等效布尔值false)时,将$str赋值为'wangjinbo',否则赋值为'wjb';


原文链接:bbs.php100.com/read-htm-tid-24239.html

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the root operator in C language? What is the root operator in C language? Mar 06, 2023 pm 02:39 PM

In the C language, there is no root operator. The built-in function "sqrt()" is used to open the root, and the syntax "sqrt(value x)" is used; for example, "sqrt(4)" is to perform the square root operation on 4. , the result is 2. sqrt() is a built-in root operation function in C language. Its operation result is the arithmetic square root of the function variable; this function can neither operate negative values ​​nor output imaginary results.

Golang error: 'invalid use of ... operator' How to solve it? Golang error: 'invalid use of ... operator' How to solve it? Jun 24, 2023 pm 05:54 PM

For Golang developers, "invaliduseof...operator" is a common error. This error usually occurs when using variable-length parameter functions. It will be detected at compile time and indicate which parts have problems. This article will introduce how to solve this error. 1. What is a variable-length parameter function? A variable-length parameter function is also called a variable-parameter function. It is a function type in the Golang language. Using variable-length parameter functions, you can define multiple ones as follows

What does % mean in Java What does % mean in Java Mar 06, 2023 pm 04:48 PM

In Java, "%" means remainder. It is a binary arithmetic operator that can perform division operations and obtain the remainder. The syntax is "operand 1 % operand 2". The operand of the remainder operator "%" is usually a positive integer or a negative number or even a floating point number. If a negative number participates in this operation, the result depends on whether the previous number is positive or negative.

Analysis of the meaning and usage of += operator in C language Analysis of the meaning and usage of += operator in C language Apr 03, 2024 pm 02:27 PM

The += operator is used to add the value of the left operand to the value of the right operand and assign the result to the left operand. It is suitable for numeric types and the left operand must be writable.

What is the meaning of '==' symbol in php What is the meaning of '==' symbol in php Mar 14, 2023 pm 07:05 PM

In PHP, the "==" symbol is a comparison operator that can compare whether two operands are equal. The syntax is "operand 1 == operand 2". The "==" operator compares and tests whether the variable on the left (expression or constant) has the same value as the variable on the right (expression or constant); it only compares the values ​​of the variables, not the data types. If the two values ​​are the same, it returns a true value; if the two values ​​are not the same, it returns a false value.

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

How to determine if two numbers are divisible in php How to determine if two numbers are divisible in php Jan 10, 2023 pm 03:12 PM

In PHP, you can use the "%" and "==" operators to determine whether two numbers are divisible; you only need to use the "%" operator to divide the two numbers to get the remainder, and then use the "==" operator Just judge whether the obtained remainder is 0. The syntax is "Number 1 % Number 2 == 0". If it is 0, it can be divisible. If it is not 0, it cannot be divisible.

Magic methods in Python Magic methods in Python Apr 13, 2023 am 10:25 AM

Magic methods in Python are special methods that allow you to add "magic" to a class. They are often named surrounded by two underscores. Python's magic method, also known as the dunder (double underline) method. Most of the time, we use them for simple things like constructors (init), string representations (str, repr) or arithmetic operators (add/mul). In fact, there are many methods that you may not have heard of but are very useful. In this article, we will sort out these magic methods! We all know the size of the iterator __len__ method, which can be used in container classes Implement the len() function on. However, if you want to get the length of a class object that implements iterator

See all articles