PHP operators

In PHP, the assignment operator = is used to assign values ​​to variables.

In PHP, the arithmetic operator + is used to add values ​​together.

PHP Arithmetic Operator

Symbol Description                       Example

+                                                                                                                                           $x +                                     Multiplication sign, multiply by $x * $y
/         Division sign, divided by                                                                                                                            Different results obtained by arithmetic operators:

Example

<?php 
$x = 10; 
$y = 6;
echo ($x + $y); // 输出16
echo ($x - $y); // 输出4
echo ($x * $y); // 输出60
echo ($x / $y); // 输出1.6666666666667 
echo ($x % $y); // 输出4 
?>

Try it »

PHP7+ version has a new integer division operator intdiv(), learn about it here. Usage example:

<?php 
var_dump(intdiv(10, 3)); 
?>

The above example will output:

int(3)

PHP assignment operator

in PHP , the basic assignment operator is "=". It means that the left operand is set to the value of the right-hand expression. That is, the value of "$x = 5" is 5.

Symbol            

Example            

Equivalent equation

#=                                                                                                                                                                                                Is set to the value of the expression on the right

##+=           $x +=  $y                                                                                                  y $x = $x - $y *= $y / $y %= $x %= $y $x = $x % $y

.= $y $x .=$y $x = $x . $y

The following examples demonstrate different results obtained by using different assignment operators:
Examples

<?php 
$x = 10; 
echo $x; // 输出10
$y = 20; 
$y += 100;
echo $y; // 输出120
$z = 50;
$z -= 25;
echo $z; // 输出25
$i = 5;
$i *= 6;
echo $i; // 输出30
$j = 10;
$j /= 5;
echo $j; // 输出2
$k = 15;
$k %= 4;
echo $k; // 输出3
?>

Try it out»

The following examples demonstrate different results obtained by using different string operators:

Example

<?php
$a = "Hello";
$b = $a . " world!";
echo $b; //输出Hello world! 
$x = "Hello";
$x .= " world!";
echo $x; // 输出Hello world! 
?>

Try it »

PHP Increment/Decrease Operator

Operator                                                                                                    1, then return x -later decreased back X, and then x minus 1 The following example demonstrates the results obtained by the use/decreased transport calculation symbol: ## r Check it out»

PHP Comparison Operators

Comparison operators allow you to compare two values:

Operators

##Name                                                                                                              

#x =8 returns false

##x === y is always equal to y If x is equal to y and they are of the same type, return true #X! = Y is not equal to if X is not equal to Y, then return True 5! = 8 Return True

x <> y is not equal to y If x is not equal to y, return true ! == y is not equal to y if x is not equal to y, or their types If they are not the same, return true 5!=="5" Return true

##x > y is greater than y                  5>8 returns false

x < y                       is less If x is less than y, return True 5 & LT; 8 Return True

# x & GT; = y greater or equal to if x is greater than or equal to Y, then return True 5 & GT; ;= y less than or equal to y If x is less than or equal to y, return true #
<?php
$x = 10; 
echo ++$x; // 输出11  
$y = 10; 
echo $y++; // 输出10  
$z = 5;
echo --$z; // 输出4  
$i = 5;
echo $i--; // 输出5
?>

Try it»

PHP Logical Operator

##Operator          

Name            

Description                                               

Example

x and y Logical AND             If both x and y are true, then return true                      ) Return true x or y Logical OR If at least one of x and y is true, return true x=6 y=3 (x==6 or y==5) Return true x xor y Logical XOR If one and only one of x and y is true, then return true x=6 y=3 (x==6 xor y==3) Return false

x && y Logical AND If both x and y are true, return true x=6 y=3 (x < 10 && y > 1) Return true

##x || y Logical OR If x If at least one of y and y is true, then return true x=6 y=3 !(x==y) Returns true

Here are a few examples:

Logic and

<?php
$x = 100; 
$y = "100";
var_dump($x == $y);
echo "<br>";
var_dump($x === $y);
echo "<br>";
var_dump($x != $y);
echo "<br>";
var_dump($x !== $y);
echo "<br>";
$a = 50;
$b = 90;
var_dump($a > $b);
echo "<br>";
var_dump($a < $b);
?>

Logic or

<?php 
$x = true; 
$y = false; 
//逻辑与(并且),要求两个都为true才执行真区间,所以代码中执行假区间 
if($x && $y){ 
echo '执行了真区间'; 
}else{ 
echo '执行了假区间'; 
} 
?>

logical NOT

<?php 
$x = true; 
$y = false; 
//逻辑或,有一个为真则为真 
if($x || $y){ 
echo '执行了真区间'; 
}else{ 
echo '执行了假区间'; 
} 
?>

PHP Array Operator

Operator            

Name               Description

x + y                                                                                                                                                                                                    #x === y                                                                         ’ ’ s ’ s ’ s ’ s ’s ’ s ’ s ‐ ‐ ‐ ‐ ‐ to be true if x and y have the same key/value pairs in the same order and the same type Returns true

x <> y is not equal If x is not equal to y, then return true

x !== y Not equal If x is not equal to y, then return true

The following Examples demonstrate different results using some array operators:

Example

<?php 
$y = false; 
//逻辑非,把false变为了true 
if(!$y){ 
echo '执行了真区间'; 
}else{ 
echo '执行了假区间'; 
} 
?>

Try it out»

Ternary Operator

Another conditional operator is the "?:" (or ternary) operator.

Syntax format

(expr1) ? (expr2) : (expr3)

When expr1 evaluates to TRUE, the value is expr2. The value of expr1 when evaluating to FALSE is expr3.

can be written as: $x? True code segment (only one line of code can be written): Fake code segment (only one line of code can be written);

Since PHP 5.3, the ternary can be omitted The middle part of the operator. The expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE and expr3 otherwise.

Example

In the following example, it is judged that the $_GET request contains the user value. If so, $_GET['user'] is returned, otherwise nobody is returned:

$_GET variable Will be explained in the PHP form with reference to PHP $_GET variable

<?php
$x = array("a" => "red", "b" => "green"); 
$y = array("c" => "blue", "d" => "yellow"); 
$z = $x + $y; // $x 和 $y 数组合并
var_dump($z);
var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x <> $y);
var_dump($x !== $y);
?>

Note: PHP_EOL is a newline character and is compatible with larger platforms.

There is an additional NULL merge operator in the PHP7+ version. The example is as follows:

<?php 
$test = 'php中文网'; 

//普通写法 
$username = isset($test) ? $test : 'nobody'; 
echo $username, PHP_EOL; //输出 php中文网

// PHP 5.3+ 版本写法 
$username = $test ?: 'nobody'; 
echo $username, PHP_EOL; //输出 php中文网
?>

Combined comparison operator (PHP7+)

PHP7+ supports combined comparison operator. The example is as follows:

<?php 
// 如果 $_GET['user'] 不存在返回 'nobody',否则返回 $_GET['user'] 的值 
$username = $_GET['user'] ?? 'nobody'; 
// 类似的三元运算符 
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; 
?>

Operator precedence

Operator precedence is a very complex rule, please refer to http://php.net/manual/zh/language.operators.precedence.php No need Memorize by rote,

We only need to understand the commonly used rules, from high to bottom:

++, -- (increasing and decreasing)

! Logical NOT

Multiplication and division operations are higher than addition and subtraction operations (multiplication and division first, then addition and subtraction)

Comparison operators (calculate first and then compare)

Logical operators (results of comparison Do logical operations)


Continuing Learning
||
<?php $x = 10; $y = 6; echo ($x + $y); // 输出16 echo "<br>"; echo ($x - $y); // 输出4 echo "<br>"; echo ($x * $y); // 输出60 echo "<br>"; echo ($x / $y); // 输出1.6666666666667 echo "<br>"; echo ($x % $y); // 输出4 ?>
submitReset Code