PHP Operators

In this example you will learn how to manipulate or perform the operations on variables and values using operators in PHP.

What are Operators in PHP

Operators are symbols that tell the PHP processor to perform certain actions. For example, the addition (+) symbol is an operator that tells PHP to add two variables or values.

PHP Arithmetic Operators

The arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

PHP Arithmetic Operators

Example

<?php
$x = 10;
$y = 4;
echo($x + $y); // OUTPUT: 14
echo($x - $y); // OUTPUT: 6
echo($x * $y); // OUTPUT: 40
echo($x / $y); // OUTPUT: 2.5
echo($x % $y); // OUTPUT: 2
?>

PHP Assignment Operators

The assignment operators are used to assign values to variables.

PHP Assignment Operators

Example

<?php
$x = 10;
echo $x; // OUTPUT: 10
 
$x = 20;
$x += 30;
echo $x; // OUTPUT: 50
 
$x = 50;
$x -= 20;
echo $x; // OUTPUT: 30
 
$x = 5;
$x *= 25;
echo $x; // OUTPUT: 125
 
$x = 50;
$x /= 10;
echo $x; // OUTPUT: 5
 
$x = 100;
$x %= 15;
echo $x; // OUTPUT: 10
?>

PHP Comparison Operators

The comparison operators are used to compare two values in a Boolean fashion.

PHP Comparison Operators

Example

<?php
$x = 25;
$y = 35;
$z = "25";
var_dump($x == $z);  // OUTPUT: boolean true
var_dump($x === $z); // OUTPUT: boolean false
var_dump($x != $y);  // OUTPUT: boolean true
var_dump($x !== $z); // OUTPUT: boolean true
var_dump($x < $y);   // OUTPUT: boolean true
var_dump($x > $y);   // OUTPUT: boolean false
var_dump($x <= $y);  // OUTPUT: boolean true
var_dump($x >= $y);  // OUTPUT: boolean false
?>

PHP Incrementing and Decrementing Operators

PHP Incrementing and Decrementing Operators

Example

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

PHP Logical Operators

The logical operators are typically used to combine conditional statements.

PHP Logical Operators

Example

<?php
$year = 2014;
// 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.";
}
?>

PHP String Operators

There are two operators which are specifically designed strings.

PHP String Operators

Example

<?php
$x = "Hello";
$y = " World!";
echo $x . $y; // OUTPUT: Hello World!
 
$x .= $y;
echo $x; // OUTPUT: Hello World!
?>

PHP Array Operators

The array operators are used to compare arrays:

PHP Array Operators

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);   // OUTPUT: boolean false
var_dump($x === $y);  // OUTPUT: boolean false
var_dump($x != $y);   // OUTPUT: boolean true
var_dump($x <> $y);   // OUTPUT: boolean true
var_dump($x !== $y);  // OUTPUT: boolean true
?>

I hope you understand the various operators in PHP.

Leave a Reply