Home > Backend Development > PHP Tutorial > code - Some questions about operators in php?

code - Some questions about operators in php?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-06 13:53:55
Original
1052 people have browsed it

In php (or in other languages?), similar to

<code>if(!$a=b){
    .....
}</code>
Copy after login
Copy after login

or

<code>empty($a)&&$a=array()</code>
Copy after login
Copy after login
What does

mean? What are the benefits of writing this way?

Reply content:

In php (or in other languages?), similar to

<code>if(!$a=b){
    .....
}</code>
Copy after login
Copy after login

or

<code>empty($a)&&$a=array()</code>
Copy after login
Copy after login
What does

mean? What are the benefits of writing this way?

Don’t write like this.
For example, I have been writing code for several years, and I don’t know the priority of ! and = at first glance

$a = someFunc();
if ($a) {
abc();
}

if ($a = someFunc()) {
abc();
}

if ( !($a = someFunc()) ) {
abc();
}

if ( ! $a = someFunc() ) {
abc();
}

($a = someFunc()) && abc();

Finally, use if else all the time. The code is fun for a while, but the maintenance is heartbreaking.

empty($a) && $a=array()
//这是短路运算符,如同
if(empty($a)){
   $a=array();
}
Copy after login

if(!$a=b){

<code>.....</code>
Copy after login

}
First assign value b
to $a and then!$a Just pay attention to the operator priority here

The expression before the second && is only to determine whether the variable is empty; the following expression is to determine whether it is an empty array, which can be omitted here

<code>    $a = FALSE; 
    $b = TRUE; 
    $c = 2;
    
    // $a为false,短路了,$c没有赋值20
    $a && $c=20;
    echo $c; // 2

    echo "<br />";

    // $b为true,走了下一步,$c被赋值20
    $b && $c=20;
    echo $c; // 20
</code>
Copy after login

Your code can be understood as: if $a does not exist, it is true, take the next step and assign a value to $a;

Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template