Home >Backend Development >PHP Tutorial >Compilation of PHP operator knowledge points

Compilation of PHP operator knowledge points

藏色散人
藏色散人forward
2019-09-27 09:17:212471browse

1. Arithmetic operators

1. Overview

Compilation of PHP operator knowledge points

2. TIPS

The division operator always returns a floating point number. The only exception is that both operands are integers (or integers converted from strings) and are exactly divisible, in which case it returns an integer.

The operands of the modulo operator will be converted to integers (except for the decimal part) before operation.

The result of the modulo operator % is the same as the sign (positive or negative sign) of the dividend. That is, the result of $a % $b has the same sign as $a. For example:

echo (5 % 3)."\n";        // prints 2
echo (5 % -3)."\n";        // prints 2
echo (-5 % 3)."\n";        // prints -2
echo (-5 % -3)."\n";       // prints -2

2. Bitwise operators

1. Overview

Compilation of PHP operator knowledge points

2. TIPS

Displacement is a mathematical operation in PHP.

Bits moved out in any direction are discarded.

When shifting left, the right side is filled with zeros, and the sign bit is removed, which means that the positive and negative signs are not retained.

When shifting right, the left side is filled with sign bits, which means that the positive and negative signs are retained.

Pay special attention to priority when using bitwise operators. For example:

$a & $b == true, compare first and then perform bitwise AND;

($a & $b) == true, perform bitwise AND first and then compare. .

3. Comparison operators

1. Overview

Compilation of PHP operator knowledge points

2. TIPS

If you compare a number and a string or compare a string involving numeric content, the string will be converted to a numeric value and the comparison will be performed according to the numeric value. This rule also applies to switch statements. When comparing with === or !==, no type conversion is performed because both types and values ​​are compared.

<?php
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true
switch ("a") {
case 0:
    echo "0";
    break;
case "a": // 永远不会到达a,因为 "a" 已经与 0 匹配
    echo "a";
    break;
}
?>

3. For multiple types, comparison operators compare according to the following table (in order)

Compilation of PHP operator knowledge points

##4 . Loose comparison table

Compilation of PHP operator knowledge points

4. Increment/decrement operators

1. Overview

Compilation of PHP operator knowledge points

2. TIPS

The increment/decrement operators do not affect Boolean values.

Decreasing the NULL value has no effect, but the result of incrementing NULL is 1.

When dealing with arithmetic operations on character variables, PHP follows Perl's habits rather than C's.

For example, in Perl $a = 'Z'; $a ; will turn $a into 'AA', and in C, a = 'Z'; a ; will turn a into ' [' (the ASCII value of 'Z' is 90, and the ASCII value of '[' is 91).

Note that character variables can only be incremented, not decremented, and only support pure letters (a-z and A-Z).

Incrementing/decrementing other character variables will be invalid, and the original string will not change.

5. Logical operators

1. Overview

Compilation of PHP operator knowledge points

2. TIPS

The reason why "AND" and "OR" have two different forms of operators is that their operation priorities are different.

// "&&" 比 "and" 的优先级高
// 表达式 (true && false) 的结果被赋给 $g
// 等同于:($g = (true && false))
$g = true && false;
// 常量 true 被赋给 $h,false 被忽略
// 等同于:(($h = true) and false)
$h = true and false;

6. Array operators

1. Overview

Compilation of PHP operator knowledge points

2. TIPS

If the cells in the array have the same key name and value, they will be equal when compared

$a = array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)

The difference between operator and array_merge()

When When the array subscript is a character, if the same key name is encountered, the operator will discard the subsequent value; array_merge() will overwrite the previous value with the subsequent value.

When the array subscript is a numeric value, array_merge() will retain all values ​​and re-index the array. The operator also discards subsequent values ​​with the same subscript.

$arr1 = [&#39;color&#39; => &#39;red&#39;, 10, 20];
$arr2 = [0, 1, &#39;color&#39; => &#39;green&#39;];
print_r($arr1 + $arr2);
print_r(array_merge($arr1, $arr2));

Output:

Array ( [color] => red [0] => 10 [1] => 20 ) 
Array ( [color] => green [0] => 10 [1] => 20 [2] => 0 [3] => 1 )

7. Operator precedence

1. Overview

Compilation of PHP operator knowledge points

2. TIPS

If the operators have the same precedence, the combination direction of the operators determines how to operate. For example, "-" is a left-joint, then 1 - 2 - 3 is equivalent to (1 - 2) - 3 and the result is -4. On the other hand, "=" is a right-joint, so $a = $b = $c is equivalent to $a = ($b = $c).

没有结合的相同优先级的运算符不能连在一起使用,例如 1 1 在PHP是不合法的。但另外一方面表达式 1

括号的使用,哪怕在不是必要的场合下,通过括号的配对来明确标明运算顺序,而非靠运算符优先级和结合性来决定,通常能够增加代码的可读性。

着重记忆:

递增/递减 > ! > 算术运算符 > 大小比较 > (不)相等比较 > 引用 > 位运算符(^) > 位运算符(|) > 逻辑与 > 逻辑或 > 三目 > 赋值 > and > xor > or

3. 实战例题

请写出下列程序输出的结果

$a = 0;
$b = 0;
if ($a = 3 > 0 || $b = 3 > 0) 
{
    $a++;
    $b++;
    echo $a. "\n";
    echo $b. "\n";
}

例题分析

1.题目中包含 = > || 三种运算符,优先级顺序为 > > || > =

2.$a = 3 > 0 || $b = 3 > 0 等价于 $a = ( ( 3 > 0 ) || $b = ( 3 > 0 ) )

3.( 3 > 0 ) 为 true,因此不会再运算 || 后面的式子

4.$a = true,所以程序可以改写为

$a = 0;
$b = 0;
if ($a = true) 
{
    $a++;
    $b++;
    echo $a. "\n";
    echo $b. "\n";
}

5.因为 “递增/递减运算符不影响布尔值”,所以 $a++ 后,$a 的值依然是 true,echo true,会输出 1

6.因为 $b = 0 ,所以 $b++ 后,$b 的值为 1

7.输出结果为

1
1

The above is the detailed content of Compilation of PHP operator knowledge points. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete
Previous article:php file upload failedNext article:php file upload failed