PHP

Operators

PHP / Operators

Operators

Operators

Operators in PHP are special symbols used to perform operations on variables and values, such as mathematical calculations, comparisons, and logical operations.

Types of Operators

In php operators are divided into different groups based on their functionality. They are used to perform operations on variables and values such as calculations, comparisons, and logical decisions.

Types of Operators

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


Β 

Arithmetic Operators

Arithmetic operators in PHP are special symbols used to perform basic mathematical calculations on numbers.

  • + β†’ Addition
  • - β†’ Subtraction
  • * β†’ Multiplication
  • / β†’ Division
  • % β†’ Modulus (remainder)
  • ** β†’ Exponentiation (power)

Example

$a = 10;
$b = 3;

// Addition (+)
echo "Addition: " . ($a + $b);
echo "\n";

// Subtraction (-)
echo "Subtraction: " . ($a - $b);
echo "\n";

// Multiplication (*)
echo "Multiplication: " . ($a * $b);
echo "\n";

// Division (/)
echo "Division: " . ($a / $b);
echo "\n";

// Modulus (%) - Remainder
echo "Modulus: " . ($a % $b);
echo "\n";

// Exponentiation (**) - Power
echo "Exponentiation: " . ($a ** $b);
?>

Assignment Operators

Assignment operators in PHP are special symbols used to assign values to variables and update their values using arithmetic operations.

  • = β†’ Assign
  • += β†’ Add and assign
  • -= β†’ Subtract and assign
  • *= β†’ Multiply and assign
  • /= β†’ Divide and assign
  • %= β†’ Modulus and assign

Example

$x = 10; Β // Assign value

// Addition assignment
$x += 5;
echo "After += : " . $x;
echo "\n";

// Subtraction assignment
$x -= 3;
echo "After -= : " . $x;
echo "\n";

// Multiplication assignment
$x *= 2;
echo "After *= : " . $x;
echo "\n";

// Division assignment
$x /= 2;
echo "After /= : " . $x;
echo "\n";

// Modulus assignment
$x %= 4;
echo "After %= : " . $x;
?>

Comparison Operators

Comparison operators in PHP are used to compare variables or values and return a Boolean value (true or false) based on the result of the comparison.

  • == β†’ Equal to
  • === β†’ Identical (equal value and same type)
  • != or <> β†’ Not equal to
  • !== β†’ Not identical
  • > β†’ Greater than
  • < β†’ Less than
  • >= β†’ Greater than or equal to
  • <= β†’ Less than or equal to

Example

$a = 10;
$b = 5;

// Equal to
var_dump($a == $b);
echo "\n";

// Identical
var_dump($a === $b);
echo "\n";

// Not equal
var_dump($a != $b);
echo "\n";

// Not identical
var_dump($a !== $b);
echo "\n";

// Greater than
var_dump($a > $b);
echo "\n";

// Less than
var_dump($a < $b);
echo "\n";

// Greater than or equal
var_dump($a >= $b);
echo "\n";

// Less than or equal
var_dump($a <= $b);
?>

Increment / Decrement Operators

Increment and decrement operators in PHP are used to increase or decrease a variable’s value by one step.

1. Increment Operators

  • ++$x β†’ Pre-increment (increase first, then use value)
  • $x++ β†’ Post-increment (use value first, then increase)

2. Decrement Operators

  • --$x β†’ Pre-decrement (decrease first, then use value)
  • $x-- β†’ Post-decrement (use value first, then decrease)

Example

$x = 5;

// Pre-increment
echo "Pre-increment: " . ++$x;
echo "\n";

// Post-increment
echo "Post-increment: " . $x++;
echo "\n";
echo "After Post-increment: " . $x;
echo "\n";

// Pre-decrement
echo "Pre-decrement: " . --$x;
echo "\n";

// Post-decrement
echo "Post-decrement: " . $x--;
echo "\n";
echo "After Post-decrement: " . $x;
?>

Logical Operators

Logical operators in PHP are used to combine or evaluate multiple conditions and return true or false based on the logic.

  • && β†’ AND (both conditions must be true)
  • || β†’ OR (any one condition must be true)
  • ! β†’ NOT (reverses the result)
  • and β†’ AND (low precedence)
  • or β†’ OR (low precedence)
  • xor β†’ Exclusive OR (only one condition must be true)

Example

$x = 10;
$y = 5;

// AND operator (both must be true)
var_dump($x > 5 && $y < 10);
echo "\n";

// OR operator (one must be true)
var_dump($x > 15 || $y < 10);
echo "\n";

// NOT operator (reverses result)
var_dump(!($x > 5));
echo "\n";

// AND keyword operator
var_dump($x > 5 and $y < 10);
echo "\n";

// OR keyword operator
var_dump($x > 15 or $y < 10);
echo "\n";

// XOR operator (only one condition true)
var_dump($x > 15 xor $y < 10);
?>

String Operators

string operators are used to join two or more words (strings) together or add new text to an existing string.

  • . β†’ Concatenation operator (joins strings)
  • .= β†’ Concatenation assignment operator (adds and assigns)

Example

$first = "Hello";
$second = "World";

// Concatenation (.)
echo "Using . operator: " . $first . " " . $second;
echo "\n";

// Concatenation assignment (.=)
$message = "Hello";
$message .= " PHP";

echo "Using .= operator: " . $message;
?>

Array Operators

Array operators in PHP are used to compare arrays or join them together based on their keys and values.

  • + β†’ Union (combine arrays)
  • == β†’ Equal (same key-value pairs)
  • === β†’ Identical (same key-value pairs and same order/type)
  • != β†’ Not equal
  • <> β†’ Not equal
  • !== β†’ Not identicalΒ 

Example

$a = array("a" => 10, "b" => 20);
$b = array("c" => 30, "d" => 40);
$c = array("a" => 10, "b" => 20);

// Union operator (+)
print_r($a + $b);
echo "\n";

// Equal operator (==)
var_dump($a == $c);
echo "\n";

// Identical operator (===)
var_dump($a === $c);
echo "\n";

// Not equal operator (!=)
var_dump($a != $b);
echo "\n";

// Not identical operator (!==)
var_dump($a !== $b);
?>

Conditional Operators

The conditional (ternary) operator in PHP is used to check a condition and assign one of two values depending on whether the condition is true or false.

1. Ternary Operator (?:)

A shorthand for if...else.

2. Null Coalescing Operator (??)

Used to check if a variable is set and not null.

Example

// Set age value
$age = 18;

// Get name from URL, if not set use "Guest"
$name = $_GET['name'] ?? "Guest";

// Check age using ternary operator and combine with name
$status = ($age >= 18) ? "$name is Adult" : "$name is Minor";

// Display result
echo $status;

?>

Technology
PHP
want to connect with us ?
Contact Us