PHP Operators

Operators are symbols that are used to perform certain operations on variables and values using operators.

For example, the subtraction () symbol is an operator that tells PHP to subtract two variables or values, while the greater-than ( > ) symbol is an operator that tells PHP to compare two values.
PHP divides the operators in the following form:

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





1.PHP Arithmetic operators

The arithmetic operators are used to perform arithmetical actions, like as addition, subtraction, multiplication etc. Let’s see a complete list of PHP’s arithmetic operators given below:

Operator Description Example Result
+ Addition $a + $b Sum of $a and $b
- Subtraction $a - $b Difference of $a and $b.
* Multiplication $a * $b Product of $a and $b.
/ Division $a / $b Quotient of $a and $b
% Modulus $a % $b Remainder of $a divided by $b

The following example will show you these arithmetic operators in action:

Example

<?php
$x = 10;
$y = 4;
echo($a + $b); // 0utputs: 14

echo($a - $b); // 0utputs: 6

echo($a * $b); // 0utputs: 40

echo($a / $b); // 0utputs: 2.5

echo($a % $b); // 0utputs: 2
?>




2. PHP Assignment Operators

The PHP assignment operators are used to assign values to variables.The basic assignment operator in PHP is “=” .

Operator Description Example Same As
= Assign $a = $b $a = $b
+= Add and assign $a += $b $a = $a + $b
-= Subtract and assign $a -= $b $a = $a - $b
*= Multiply and assign $a *= $b $a = $a * $b
/= Divide and assign quotient $a /= $b $a = $a / $b
%= Divide and assign modulus $a %= $b $a = $a % $b

The following example will show you all Assignment operators in action:

Example

<?php
$a = 100;
echo $a; // Outputs: 100
 
$a = 200;
$a += 30;
echo $a; // Outputs: 230
 
$a = 40;
$a -= 20;
echo $a; // Outputs: 20
 
$a = 10;
$a *= 25;
echo $a; // Outputs: 250
 
$a = 50;
$a /= 10;
echo $a; // Outputs: 5
 
$a = 100;
$a %= 15;
echo $a; // Outputs: 10
?>




3. PHP Comparison operators

The PHP comparison operators are used to compare two values (number or string):

Operator Name Example Result
== Equal $a == $b True if $a is equal to $b
=== Identical $a === $b True if $a is equal to $b, and they are of the same type
!= Not equal $a != $b True if $a is not equal to $b
<> Not equal $a <> $b True if $a is not equal to $b
!== Not identical $a !== $b True if $a is not equal to $b, or they are not of the same type
< Less than $a < $b True if $a is less than $b
> Greater than $a > $b True if $a is greater than $b
>= Greater than or equal to $a >= $b True if $a is greater than or equal to $b
<= Less than or equal to $a <= $b True if $a is less than or equal to $b

The following example will show you Comparison operators in action:

Example

<?php
$a = 25;
$y = 35;
$z = "25";
var_dump($a == $c);  // Outputs: boolean true
var_dump($a === $c); // Outputs: boolean false
var_dump($a != $b);  // Outputs: boolean true
var_dump($a !== $c); // Outputs: boolean true
var_dump($a < $b);   // Outputs: boolean true
var_dump($a > $b);   // Outputs: boolean false
var_dump($a <= $b);  // Outputs: boolean true
var_dump($a >= $b);  // Outputs: boolean false
?>




4. PHP Increment/Decrement operators

The PHP increment/decrement operators are used to increment and decrement a variable’s value.

Operator Name Results
++$a Pre-increment Increments $a by one, then returns $a
$a++ Post-increment Returns $a, then increments $a by one
--$a Pre-decrement Decrements $a by one, then returns $a
$a-- Post-decrement Returns $a, then decrements $a by one

The following example will show you Increment/Decrement operators in action:

Example

<?php
$x = 10;
echo ++$x; // Outputs: 11
echo $x;   // Outputs: 11
 
$x = 10;
echo $x++; // Outputs: 10
echo $x;   // Outputs: 11
 
$x = 10;
echo --$x; // Outputs: 9
echo $x;   // Outputs: 9
 
$x = 10;
echo $x--; // Outputs: 10
echo $x;   // Outputs: 9
?>




5. PHP Logical Operators

The PHP logical operators are mostly used to combine conditional statements.

Operator Name Example Result
and And $a and $b True if both $a and $b are true
or Or $a or $b True if either $a or $b is true
xor Xor $a xor $a True if either $a or $b is true, but not both
&& And $a && $b True if both $a and $b are true
|| Or $a || $b True if either $$a or $b is true
! Not !$a True if $a is not true

The following example will show you Logical Operators operators in action:

Example

<?php
$year = 2019;
// Leap years are divisible by 400 or by 4 but not 100
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){
    echo "$year is a leap year.";
} else{
    echo "$year is not a leap year.";
}
?>

Output:

2019 is not a leap year.




6. PHP String operators

In PHP String operators are follow two operators which are specifically designed for strings.

Operator Description Example Result
. Concatenation $str1 . $str2 Concatenation of $str1 and $str2
.= Concatenation assignment $str1 .= $str2 Appends the $str2 to the $str1

The following example will show you String Operators operators in action:

Example

<?php
$a = "Good";
$b = " Morning!";
echo $a . $b; // Outputs: Good Morning
 
$a .= $b;
echo $a; // Outputs: Good Morning!
?>

Output:

Good Morning
Good Morning




7. PHP Array operators

These array operators are used to compare arrays:

Operator Name Example Result
+ Union $a + $b Union of $a and $b
== Equality $a == $b True if $a and $b have the same key/value pairs
=== Identity $a === $b True if $a and $b have the same key/value pairs in the same order and of the same types
!= Inequality $a != $b True if $a is not equal to $b
<> Inequality $a <> $b True if $a is not equal to $b
!== Non-identity $a !== $b True if $a is not identical to $b

The following example will show you Array Operators operators in action:

Example

<?php
$x = array("a" => "Red", "b" => "Green", "c" => "Blue");
$y = array("u" => "Yellow", "v" => "Orange", "w" => "Pink");
$z = $x + $y; // Union of $x and $y
var_dump($z);
var_dump($x == $y);   // Outputs: boolean false
var_dump($x === $y);  // Outputs: boolean false
var_dump($x != $y);   // Outputs: boolean true
var_dump($x <> $y);   // Outputs: boolean true
var_dump($x !== $y);  // Outputs: boolean true
?>

Pin It on Pinterest

Shares
Share This