PHP complete se...login
PHP complete self-study manual
author:php.cn  update time:2022-04-15 13:53:54

PHP operators




In this chapter we will discuss the application of different operators in PHP.

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

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


What are the PHP operators?

php operators include arithmetic operators, assignment operators, increment\decrement operators, comparison operators, logical operators, array operators, ternary operators, and combined comparison operators.


PHP Arithmetic Operators

##x / yDivide the quotient of x and y15 / 5 3x % yModulo (remainder of division)Remainder of x divided by y5 % 21##- xa . b

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

Example

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

Running Example»

Click the "Run Example" button to view the online example

PHP7+ version has a new integer division operatorintdiv(), 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.

OperatorNameDescription ExampleResult
x + yAdd the sum of x and y 2 + 24
x - yminus the difference between x and y5 - 23
x * ymultiplied by the product of x and y5 * 210
10 % 8
10 % 2
2
0
Negationx Negation- 2-2
JuxtapositionConnect two strings"Hi" . "Ha"HiHa
Operator is equivalent to Description
x = y x = yThe left operand is set to the value of the right-hand expression
x += yx = x + yplus
x -= yx = x - yminus
x *= y#x = x * y times
x /= yx = x / yDivision
x %= yx = x % ymodulo (remainder of division)
a .= ba = a . bConcatenate two strings

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

Example

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

Run Example»

Click "Run Example" button to view online examples

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

Example

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

Run instance»

Click the "Run instance" button to view the online instance


##PHP increment/decrement operator

OperatorNameDescription##++ xx ++ to return x, then x decreases by 1
Pre-increment x adds 1, then returns x
and then increments returns #-- Then decrement

The following example demonstrates the results obtained using the increment/decrement operator:

Example

<?php
$x=10; 
echo ++$x; // 输出11
echo "<br>";
$y=10; 
echo $y++; // 输出10
echo "<br>";
$z=5;
echo --$z; // 输出4
echo "<br>";
$i=5;
echo $i--; // 输出5
?>

Running Example»

Click the "Run Example" button to view the online example


PHP Comparison Operator

The comparison operator allows you to compare two values:

##x < y is less than If x is less than y, return true5< 8 Return truex >= y is greater than or equal to If x is greater than or equal to y, return true 5>=8 returns false##x <= y

The following examples demonstrate the different results obtained using some comparison operators:

Example

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

Running Example»

Click the "Run Instance" button to view the online instance


PHP logical operator

OperatorNameDescriptionInstance
x == yEqualIf x is equal to y, return true5==8 Return false
x === yConstantly equalIf x is equal to y, and they are of the same type, return true5==="5" Return false
x != yNot equal toIf x is not equal to y, return true5!=8 Return true
x <> yis not equal toIf x is not equal to y, return true5<>8 Return true
x !== yNot equal to If x is not equal to y, or their types are not the same, return true5! == "5" returns true
x > y is greater than If x is greater than y, returns true 5>8 Return false
is less than or equal to If x is less than or equal to y, returns true 5<=8 returns true
##x || y or If at least one of x and y is true, return true x=6##! xy=3
Operator NameDescriptionInstance
x and yandIf both x and y are true , then return truex=6
y=3
(x < 10 and y > 1) Return true
x or y orIf at least one of x and y is true, return truex=6
y=3
(x==6 or y==5) Return true
x xor yXORIf one and only one of x and y is true, then return truex=6
y=3
(x==6 xor y==3) returns false
x && y andIf x and y are both true, return truex=6
y=3
(x < 10 && y > 1) Return true
y=3
(x==5 || y==5) returns false
nonIf x is not true, return truex=6!(x==y) Return true

PHP Array Operator

Operatorx + yx == yx === yx != y##x <> yNot equalIf x is not equal to y, return true x !== yNot identicalIf x is not equal to y, return true

The following examples demonstrate different results obtained using some array operators:

Example

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

Running Example»

Click the "Run Example" button to view the online example


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.

Since PHP 5.3, the middle part of the ternary operator can be omitted. 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:

<?php
$test = 'php中文网';
// Common writing method
$username = isset($test) ? $test : 'nobody';
echo $username, PHP_EOL ;

// PHP 5.3+ version writing method
$username = $test ?: 'nobody';
echo $username, PHP_EOL;
?>

php中文网
php中文网

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

In the PHP7+ version, there is an additional NULL merge operator. The example is as follows:

<?php
// If $_GET['user'] does not exist, return ' nobody', otherwise return the value of $_GET['user']
$username = $_GET['user'] ?? 'nobody';
// Similar ternary operator
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
?>

Combined comparison operator (PHP7+)

PHP7+ supports combined comparison operators, examples are as follows:

<?php
// Integer
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Floating point type
echo 1.5 <=> 1.5 ; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// String
echo " a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1

?>

NameDescription
The setx and the set of y
are equal If x and y have the same key/value pair, return true
Identityif If x and y have the same key/value pair, and the order is the same and the type is the same, then true is returned
is not equal If x is not equal to y, return true