Latest web development tutorials

# PHP Operators

This chapter we will discuss the different operators of PHP applications.

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

In PHP, the arithmetic operators + for the values ​​together.

## PHP Arithmetic Operators

Operators name description Examples result
x + y plus x and y and 2 + 2 4
x - y Less The difference between x and y 5--2 3
x * y Multiply x and y plot 5 * 2 10
x / y except x and y Suppliers 15/5 3
x% y Modulus (remainder of the division) x divided by y number 5% 2
8 10%
2 10%
1
2
0
- X Negate x negated - 2
a. b Apposition Concatenating two strings "Hi". "Ha" HiHa

The following example illustrates the use of different arithmetic operators get a different result:

## Examples

<? Php
\$ X = 10;
\$ Y = 6;
echo (\$ x + \$ y); // output 16
echo (\$ x - \$ y); // output 4
echo (\$ x * \$ y); // output 60
echo (\$ x / \$ y); // output 1.6666666666667
echo (\$ x% \$ y); // output 4
?>

try it"

PHP7 + version of the new operator divisible intdiv (), use examples:

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

The above examples will output:

```int(3)
```

## PHP Assignment Operators

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

Operators Equivalent to description
x = y x = y Left operand is set to the value of the right of expression
x + = y x = x + y plus
x - = y x = x - y Less
x * = y x = x * y Multiply
x / = y x = x / y except
x% = y x = x% y Modulus (remainder of the division)
a. = b a = a. b Concatenating two strings

The following example demonstrates the use of the different results obtained by different assignment operator:

## Examples

<? Php
\$ X = 10;
echo \$ x; // output 10

\$ Y = 20;
\$ Y + = 100;
echo \$ y; // output 120

\$ Z = 50;
\$ Z - = 25;
echo \$ z; // output 25

\$ I = 5;
\$ * = 6;
echo \$ i; // output 30

\$ J = 10;
\$ J / = 5;
echo \$ j; // output 2

\$ K = 15;
\$ K% = 4;
echo \$ k; // output 3
?>

try it"

The following example illustrates the use of different string operators get a different result:

## Examples

<? Php
\$ A = "Hello";
\$ B = \$ a "world!".;
echo \$ b; // output Hello world!

\$ X = "Hello";
\$ X = "world!".;
echo \$ x; // output Hello world!
?>

try it"

## PHP increment / decrement operators

Operators name description
++ X Preincrement x plus 1, and then return x
x ++ Post-increment Returns x, then x plus 1
- X Predecrement x minus 1, and then returns x
x - After descending Returns x, then x minus 1

The following example illustrates the result of using increment / decrement operators obtained:

## Examples

<? Php
\$ X = 10;
echo ++ \$ x; // output 11

\$ Y = 10;
echo \$ y ++; // output 10

\$ Z = 5;
echo - \$ z; // output 4

\$ I = 5;
echo \$ i--; // output 5
?>

try it"

## PHP comparison operators

Comparison operator allows you to compare two values:

Operators name description Examples
x == y equal If x equals y, return true 5 == 8 returns false
x === y Always equals If x equals y, and they are the same type, it returns true === 5 '5 "returns false
x! = y not equal to If x is not equal to y, return true 5! = 8 returns true
x <> y not equal to If x is not equal to y, return true 5 <> 8 returns true
x! == y It is not identically equal to If x is not equal to y, or they are not the same type, it returns true 5! == "5" returns true
x> y more than the If x is greater than y, return true 5> 8 returns false
x <y Less than If x is less than y, return true 5 <8 returns true
x> = y greater or equal to If x is greater than or equal to y, return true 5> = 8 returns false
x <= y Less than or equal If x is less than or equal to y, return true 5 <= 8 returns true

The following example demonstrates the use of some of the different results obtained by comparison:

## Examples

<? 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);
?>

try it"

## PHP Logical Operators

Operators name description Examples
x and y versus If x and y are true, returns true x = 6
y = 3
(X <10 and y> 1) Returns true
x or y or If x and y have at least one is true, return true x = 6
y = 3
(X == 6 or y == 5) returns true
x xor y or If x and y and only one is true, return true x = 6
y = 3
(X == 6 xor y == 3) return false
x && y versus If x and y are true, returns true x = 6
y = 3
(X <10 && y> 1) Returns true
x || y or If x and y have at least one is true, return true x = 6
y = 3
(X == 5 || y == 5) returns false
! X non- If x is not true, it returns true x = 6
y = 3
! (X == y) returns true

## PHP Array Operators

Operators name description
x + y set Set of x and y
x == y equal If x and y have the same key / value pairs, it returns true
x === y Identical If x and y have the same key / value pairs in the same order of the same type, it returns true
x! = y not equal If x is not equal to y, return true
x <> y not equal If x is not equal to y, return true
x! == y Nonidentical If x is not equal to y, return true

The following example demonstrates the use of an array of some of the operators get a different result:

## Examples

<? Php
\$ X = array ( "a" => "red", "b" => "green");
\$ Y = array ( "c" => "blue", "d" => "yellow");
\$ Z = \$ x + \$ y; // \$ x and \$ y array merge
var_dump (\$ z);
var_dump (\$ x == \$ y);
var_dump (\$ x === \$ y);
var_dump (\$ x = \$ y!);
var_dump (\$ x <> \$ y);
var_dump (! \$ x == \$ y);
?>

try it"

## Ternary operator

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

### Syntax

```(expr1) ? (expr2) : (expr3)
```

For expr1 evaluates expr2 is TRUE, the value expr3 in expr1 evaluates to FALSE when.

Since PHP 5.3 onwards, you can omit the middle part of the ternary operator. Expression expr1:? Expr3 returns expr1 when expr1 evaluates to TRUE, otherwise it returns expr3.

### Examples

The following examples are contained in the user determine the value by \$ _GET request, if there is return \$ _GET [ 'user'], otherwise nobody:

```<?php
\$test = '本教程';
// 普通写法
\$username = isset(\$test) ? \$test : 'nobody';

// PHP 5.3+ 版本写法
?>
```
```本教程

Note: PHP_EOL is a newline, more compatible platform.

In PHP7 + version more than a NULL coalescing operator, examples are as follows:

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

## A combination of comparison operators (PHP7 +)

PHP7 + support combination comparison operators, examples are as follows:

```<?php
// 整型
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// 浮点型
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// 字符串
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
?>
```