WikiGalaxy

Personalize

PHP Operators

PHP operators are crucial in performing operations on variables and values. They are used to manipulate data and variables, perform calculations, and control the flow of a PHP script. Below are detailed explanations and examples of various PHP operators: ### Arithmetic Operators Arithmetic operators in PHP are used to perform common mathematical operations such as addition, subtraction, multiplication, etc.

Arithmetic Operators

Addition (+)

Adds two operands. For example, $a + $b results in the sum of $a and $b.

Subtraction (-)

Subtracts the second operand from the first. For example, $a - $b results in the difference between $a and $b.

Multiplication (*)

Multiplies two operands. For example, $a * $b results in the product of $a and $b.

Division (/)

Divides the first operand by the second. For example, $a / $b results in the quotient of $a divided by $b.

Modulus (%)

Returns the remainder of the division of the first operand by the second. For example, $a % $b results in the remainder of $a divided by $b.

Exponentiation (**)

Raises the first operand to the power of the second operand. For example, $a ** $b results in $a raised to the power of $b.


      <?php
        $a = 10;
        $b = 2;
        echo $a + $b; // Outputs: 12
        echo $a - $b; // Outputs: 8
        echo $a * $b; // Outputs: 20
        echo $a / $b; // Outputs: 5
        echo $a % $b; // Outputs: 0
        echo $a ** $b; // Outputs: 100
      ?>
    

Console Output:

12

8

20

5

0

100

### Assignment Operators Assignment operators are used to assign values to variables in PHP.

Assignment Operators

Basic Assignment (=)

Assigns the right operand to the left operand. For example, $a = $b assigns the value of $b to $a.

Add and Assign (+=)

Adds the right operand to the left operand and assigns the result to the left operand. For example, $a += $b is equivalent to $a = $a + $b.

Subtract and Assign (-=)

Subtracts the right operand from the left operand and assigns the result to the left operand. For example, $a -= $b is equivalent to $a = $a - $b.

Multiply and Assign (*=)

Multiplies the right operand with the left operand and assigns the result to the left operand. For example, $a *= $b is equivalent to $a = $a * $b.

Divide and Assign (/=)

Divides the left operand by the right operand and assigns the result to the left operand. For example, $a /= $b is equivalent to $a = $a / $b.

Modulus and Assign (%=)

Takes modulus using two operands and assigns the result to the left operand. For example, $a %= $b is equivalent to $a = $a % $b.


      <?php
        $a = 10;
        $b = 5;
        $a += $b; // $a = 15
        $a -= $b; // $a = 10
        $a *= $b; // $a = 50
        $a /= $b; // $a = 10
        $a %= $b; // $a = 0
      ?>
    

Console Output:

15

10

50

10

0

### Comparison Operators Comparison operators are used to compare two values in PHP.

Comparison Operators

Equal (==)

Checks if two operands are equal. Returns true if they are equal. For example, $a == $b.

Identical (===)

Checks if two operands are equal and of the same type. Returns true if they are identical. For example, $a === $b.

Not Equal (!=)

Checks if two operands are not equal. Returns true if they are not equal. For example, $a != $b.

Not Identical (!==)

Checks if two operands are not identical. Returns true if they are not identical. For example, $a !== $b.

Greater Than (>)

Checks if the left operand is greater than the right operand. Returns true if it is. For example, $a > $b.

Less Than (<)

Checks if the left operand is less than the right operand. Returns true if it is. For example, $a < $b.


      <?php
        $a = 10;
        $b = 20;
        var_dump($a == $b); // bool(false)
        var_dump($a === $b); // bool(false)
        var_dump($a != $b); // bool(true)
        var_dump($a !== $b); // bool(true)
        var_dump($a < $b); // bool(true)
        var_dump($a > $b); // bool(false)
      ?>
    

Console Output:

bool(false)

bool(false)

bool(true)

bool(true)

bool(true)

bool(false)

### Logical Operators Logical operators are used to combine conditional statements in PHP.

Logical Operators

And (&&)

Returns true if both operands are true. For example, $a && $b.

Or (||)

Returns true if either of the operands is true. For example, $a || $b.

Not (!)

Returns true if the operand is false. For example, !$a.

And (and)

Returns true if both operands are true. This is a lower precedence version of &&. For example, $a and $b.

Or (or)

Returns true if either of the operands is true. This is a lower precedence version of ||. For example, $a or $b.

Xor (xor)

Returns true if either of the operands is true, but not both. For example, $a xor $b.


      <?php
        $a = true;
        $b = false;
        var_dump($a && $b); // bool(false)
        var_dump($a || $b); // bool(true)
        var_dump(!$a); // bool(false)
        var_dump($a and $b); // bool(false)
        var_dump($a or $b); // bool(true)
        var_dump($a xor $b); // bool(true)
      ?>
    

Console Output:

bool(false)

bool(true)

bool(false)

bool(false)

bool(true)

bool(true)

### String Operators String operators are used to manipulate string values in PHP.

String Operators

Concatenation (.)

Joins two strings together. For example, $a . $b concatenates $a and $b.

Concatenation Assignment (.=)

Appends the right operand to the left operand and assigns the result to the left operand. For example, $a .= $b is equivalent to $a = $a . $b.


      <?php
        $a = "Hello";
        $b = "World";
        echo $a . " " . $b; // Outputs: Hello World
        $a .= " PHP";
        echo $a; // Outputs: Hello PHP
      ?>
    

Console Output:

Hello World

Hello PHP

### Increment/Decrement Operators Increment and decrement operators are used to increase or decrease the value of a variable by one.

Increment/Decrement Operators

Increment (++)

Increases an integer value by one. For example, ++$a or $a++.

Decrement (--)

Decreases an integer value by one. For example, --$a or $a--.


      <?php
        $a = 5;
        echo ++$a; // Outputs: 6
        echo $a++; // Outputs: 6
        echo $a;   // Outputs: 7
        echo --$a; // Outputs: 6
        echo $a--; // Outputs: 6
        echo $a;   // Outputs: 5
      ?>
    

Console Output:

6

6

7

6

6

5

### Array Operators Array operators are used to perform operations on arrays in PHP.

Array Operators

Union (+)

Combines two arrays. For example, $a + $b returns the union of $a and $b.

Equality (==)

Checks if two arrays have the same key/value pairs. For example, $a == $b.

Identity (===)

Checks if two arrays have the same key/value pairs in the same order and of the same types. For example, $a === $b.

Inequality (!=)

Checks if two arrays are not equal. For example, $a != $b.

Non-identity (!==)

Checks if two arrays are not identical. For example, $a !== $b.


      <?php
        $a = array("a" => "red", "b" => "green");
        $b = array("c" => "blue", "d" => "yellow");
        $c = $a + $b; // Union of $a and $b
        var_dump($c);
        var_dump($a == $b); // bool(false)
        var_dump($a === $b); // bool(false)
        var_dump($a != $b); // bool(true)
        var_dump($a !== $b); // bool(true)
      ?>
    

Console Output:

array(4) { ["a"]=> string(3) "red" ["b"]=> string(5) "green" ["c"]=> string(4) "blue" ["d"]=> string(6) "yellow" }

bool(false)

bool(false)

bool(true)

bool(true)

By understanding and utilizing these PHP operators, developers can effectively manipulate data, perform calculations, and control the flow of their PHP applications. Each operator serves a specific purpose and can be combined to achieve complex functionality within scripts.
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025