Spex3
    

PHP Operators


    

    

PHP Operators
Operators in PHP perform operations on variables and values. The main types of operators are:

Arithmetic Operators

Comparison Operators

Logical Operators

Assignment Operators

1. Arithmetic Operators
Used for mathematical calculations.


Operator Symbol Example ($a = 10, $b = 5) Output
Addition + $a + $b 15
Subtraction - $a - $b 5
Multiplication * $a * $b 50
Division / $a / $b 2
Modulus (Remainder) % $a % $b 0
Exponentiation ** $a ** $b 100000


✅ Example:


<?php
$a = 10;
$b = 5;
echo $a + $b; // Output: 15
?>


2. Comparison Operators
Used to compare two values.

Operator Symbol Example ($a = 10, $b = "10") Output
Equal == $a == $b true
Identical (Same Type) === $a === $b false
Not Equal != or <> $a != $b false
Not Identical !== $a !== $b true
Greater Than > $a > $b false
Less Than < $a < $b false
Greater or Equal >= $a >= $b true
Less or Equal <= $a <= $b true


✅ Example:


<?php
$a = 10;
$b = "10";

var_dump($a == $b); // true (same value)
var_dump($a === $b); // false (different type)
?>


3. Logical Operators
Used in conditions to combine multiple boolean expressions.

Operator Symbol Example ($x = true, $y = false) Output
AND && $x && $y false
AND (alternative) and $x and $y false
OR ` `
OR (alternative) or $x or $y true
NOT ! !$x false
XOR xor $x xor $y true


✅ Example:


<?php
$x = true;
$y = false;

var_dump($x && $y); // false
var_dump($x || $y); // true
var_dump(!$x); // false
?>


4. Assignment Operators
Used to assign values to variables.

Operator Symbol Example ($a = 10) Equivalent To
Assign = $a = 10 $a = 10
Add & Assign += $a += 5 $a = $a + 5
Subtract & Assign -= $a -= 5 $a = $a - 5
Multiply & Assign *= $a *= 5 $a = $a * 5
Divide & Assign /= $a /= 5 $a = $a / 5
Modulus & Assign %= $a %= 5 $a = $a % 5


✅ Example:


<?php
$a = 10;
$a += 5; // Same as $a = $a + 5;
echo $a; // Output: 15
?>


Conclusion
Arithmetic operators perform calculations.

Comparison operators compare values.

Logical operators handle conditions.

Assignment operators simplify variable assignments.


    Date: 2025-03-28 00:00:00.000000