php
PHP Operators
PHP Operators are used to perform operations on variables and values. PHP Operators can be categorized in following groups :

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
PHP Arithmetic operators

The PHP arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication, division, Modulus. The all PHP arithmetic operators are following :

Operator Name Example Result
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y
<!DOCTYPE html>
<html>
<head>
	<title>PHP Arithmetic operators</title>
</head>

<body>

<?php
$x = 9;
$y = 7;


echo "Addition : ".($x + $y);
echo "<br />(or)<br />";

echo ($x + $y);
echo "<br /><br />";

echo "Subtraction : ".($x - $y);
echo "<br /><br />";

echo "Multiplication : ".($x * $y);
echo "<br /><br />";

echo "Division : ".($x / $y);
echo "<br /><br />";

echo "Modulus : ".($x % $y);
?>

</body>
</html>
Output :
PHP Assignment operators

The assignment operators are used to assign values to variables.

Operator Expression Description
x = y x = y Assign
$x+= $y $x = $x + $y Adds 2 numbers and assigns the result to the first.
$x-= $y $x = $x -$y Subtracts 2 numbers and assigns the result to the first.
$x*= $y $x = $x*$y Multiplies 2 numbers and assigns the result to the first.
$x/= $y $x =$x/$y Divides 2 numbers and assigns the result to the first.
$x%= $y $x = $x%$y Computes the modulus of 2 numbers and assigns the result to the first.
<!DOCTYPE html>
<html>
<head>
	<title>PHP Assignment operators</title>
</head>

<body>

<?php
$x = 9;
echo $x;
echo "<br />";

$x = 15;
$x += 7;
echo $x;
echo "<br />";
 
$x = 35;
$x -= 10;
echo $x;
echo "<br />";
 
$x = 7;
$x *= 20;
echo $x;
echo "<br />";
 
$x = 45;
$x /= 27;
echo $x;
echo "<br />";
 
$x = 54;
$x %= 23;
echo $x;
?>

</body>
</html>
Output :
PHP Comparison operators

The comparison operators are used to compare two values (numbers or strings) as arguments and evaluate to either TRUE or FALSE.

Operator Name Example Result
== Equal $x == $y True if $x is equal to $y
=== Identical $x === $y True if $x is equal to $y , and they are of the same type
!= Not equal $x != $y True if $x is not equal to $y
<> Not equal $x <> $y True if $x is not equal to $y
!== Not identical $x !== $y True if $x is not equal to $y , or they are not of the same type
< Less than $x < $y True if $x is less than $y
> Greater than $x > $y True if $x is greater than $y
>= Greater than or equal to $x >= $y True if $x is greater than or equal to $y
<= Less than or equal to $x <= $y True if $x is less than or equal to $y
<!DOCTYPE html>
<html>
<head>
	<title>PHP Comparison operators</title>
</head>

<body>

<?php
$x = 18;
$y = 25;
$z = "18";

var_dump($x == $z);
echo "<br>";

var_dump($x === $z);
echo "<br>";

var_dump($x != $y);
echo "<br>";

var_dump($x !== $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);
?>

</body>
</html>
Output :
PHP Increment/Decrement operators

The increment/decrement operators are used to increment/decrement a variable's value.

Operator Name Description
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x , then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x , then decrements $x by one
<!DOCTYPE html>
<html>
<head>
	<title>PHP Increment/Decrement operators</title>
</head>

<body>

<?php
$x = 9;
echo ++$x;
echo "<br />";
echo $x;
echo "<br /><br />";

$x = 9;
echo $x++;
echo "<br />";
echo $x;
echo "<br /><br />";

$x = 9;
echo --$x;
echo "<br />";
echo $x;
echo "<br /><br />";

$x = 9;
echo $x--;
echo "<br />";
echo $x;
?>
</body>
</html>
Output :
PHP Logical operators

The logical operators are typically used to combine conditional statements.

Operator Name Example Result
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true
<!DOCTYPE html>
<html>
<head>
	<title>PHP Logical operators</title>
</head>

<body>

<?php
$year = 2020;
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){
    echo "$year is a leap year.";
} else{
    echo "$year is not a leap year.";
}
?>

</body>
</html>
Output :
PHP String operators

There are two string operators : concatenation operator ('.') and concatenating assignment operator ('.=').

Operator Name Example Result
. Concatenation $str1 . $str2 Concatenation of $str1 and $str2
.= Concatenation assignment $str1 .= $str2 Appends the $str2 to the $str1
<!DOCTYPE html>
<html>
<head>
	<title>PHP String operators</title>
</head>

<body>

<?php
	$x = "Free Time";
	$y = " Learning..!";
	echo $x . $y;
	echo "<br /><br />";
	
	$x .= $y;
	echo $x;
?>

</body>
</html>
Output :
PHP Array operators

The PHP array operators are used to compare arrays.

Operator Name Example Result
+ Union $x + $y Union of $x and $y
== Equality $x == $y True if $x and $y have the same key/value pairs
=== Identity $x === $y True if $x and $y have the same key/value pairs in the same order and of the same types
!= Inequality $x != $y True if $x is not equal to $y
<> Inequality $x <> $y True if $x is not equal to $y
!== Non-identity $x !== $y True if $x is not identical to $y
<!DOCTYPE html>
<html>
<head>
	<title>PHP Array operators</title>
</head>

<body>

<?php
$x = array("a" => "HTML", "b" => "CSS", "c" => "JavaScript");
$y = array("d" => "HTML5", "e" => "CSS3", "f" => "AngularJS");
$z = $x + $y;
var_dump($z);
echo "<hr>";

var_dump($x == $y);
echo "<br /><br />";

var_dump($x === $y);
echo "<br /><br />";

var_dump($x != $y);
echo "<br /><br />";

var_dump($x <> $y);
echo "<br /><br />";

var_dump($x !== $y);
?>

</body>
</html>
Output :

We see you’re using an ad blocker.

Our website is made possible by displaying ads to our visitors.

Please help us continue to provide you with free. So please disabling your ad blocker.