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

SymbolDescriptionExample

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

Example

Try it »

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

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

Try it out»

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

Example

Try it »

PHP Increment/Decrease Operator

Operator 1, then returnx -later decreased back X, and then x minus 1The 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 #

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 truex 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 truex 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

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

Logic or

logical NOT

PHP Array Operator

Operator

NameDescription

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

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

 "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:

Combined comparison operator (PHP7+)

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

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
||
"; echo ($x - $y); // 输出4 echo "
"; echo ($x * $y); // 输出60 echo "
"; echo ($x / $y); // 输出1.6666666666667 echo "
"; echo ($x % $y); // 输出4 ?>
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!