PHP Operators


Operators in PHP are symbols or constructs that perform operations on variables and values. They are essential for mathematical calculations, logical comparisons, assignments, and string manipulations. Understanding PHP operators is critical for writing efficient and accurate code.

Types of PHP Operators

PHP operators are divided into several categories:

  1. Arithmetic Operators – Perform mathematical operations.

  2. Assignment Operators – Assign values to variables.

  3. Comparison Operators – Compare two values.

  4. Increment/Decrement Operators – Increase or decrease a value by 1.

  5. Logical Operators – Combine conditional statements.

  6. String Operators – Manipulate strings.

  7. Array Operators – Compare arrays.

  8. Ternary Operator – Short-hand for conditional statements.

Arithmetic Operators

Arithmetic operators perform basic calculations:

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

echo $a + $b . "<br>"; // Addition: 13
echo $a - $b . "<br>"; // Subtraction: 7
echo $a * $b . "<br>"; // Multiplication: 30
echo $a / $b . "<br>"; // Division: 3.3333
echo $a % $b . "<br>"; // Modulus: 1
echo $a ** $b . "<br>"; // Exponent: 1000
?>
  • +, -, *, /, %, ** are commonly used in calculations.

  • Modulus % returns the remainder, and exponent ** calculates powers.

Assignment Operators

Assignment operators combine assignment with arithmetic operations:

<?php
$num = 10;
$num += 5; // $num = $num + 5
echo $num . "<br>"; // 15

$num *= 2; // $num = $num * 2
echo $num . "<br>"; // 30
?>
  • Other operators: -=, /=, %=, **=.

  • Useful for updating values efficiently.

Comparison Operators

Comparison operators evaluate relationships between values:

<?php
$x = 5;
$y = 10;

var_dump($x == $y); // Equal: false
var_dump($x != $y); // Not equal: true
var_dump($x > $y);  // Greater than: false
var_dump($x < $y);  // Less than: true
var_dump($x >= $y); // Greater or equal: false
var_dump($x <= $y); // Less or equal: true
var_dump($x === $y); // Identical (value and type): false
var_dump($x !== $y); // Not identical: true
?>
  • == checks value equality.

  • === checks value and type equality.

  • Comparison operators are crucial in conditional statements and loops.

Increment and Decrement Operators

Increment and decrement operators increase or decrease a value by 1:

<?php
$num = 10;
echo $num++ . "<br>"; // Post-increment: prints 10, then increments
echo ++$num . "<br>"; // Pre-increment: increments first, prints 12
echo $num-- . "<br>"; // Post-decrement: prints 12, then decrements
echo --$num . "<br>"; // Pre-decrement: decrements first, prints 10
?>
  • Pre-increment/decrement modifies the value before evaluation.

  • Post-increment/decrement modifies the value after evaluation.

  • Useful in loops and counters.

Logical Operators

Logical operators are used to combine conditional statements:

<?php
$a = true;
$b = false;

var_dump($a && $b); // AND: false
var_dump($a || $b); // OR: true
var_dump(!$a);      // NOT: false
var_dump($a xor $b); // XOR: true
?>
  • && or AND – True if both are true.

  • || or OR – True if at least one is true.

  • ! – Negates the boolean value.

  • xor – True if only one is true.

String Operators

PHP uses the concatenation operator . to join strings:

<?php
$first = "Hello, ";
$second = "Vicky!";
echo $first . $second; // Outputs: Hello, Vicky!
?>
  • .= appends a string to an existing variable:

$text = "Hello";
$text .= " World!";
echo $text; // Outputs: Hello World!
  • Useful for building dynamic messages or HTML content.

Array Operators

Array operators compare arrays:

<?php
$array1 = ["a" => "Apple", "b" => "Banana"];
$array2 = ["b" => "Banana", "a" => "Apple"];

var_dump($array1 == $array2); // True: Same key-value pairs
var_dump($array1 === $array2); // False: Different order
var_dump($array1 != $array2);  // False
var_dump($array1 <> $array2);  // False
?>
  • + operator merges arrays, preserving keys from the first array.

  • Array operators are important when comparing configuration or dataset arrays.

Ternary Operator

The ternary operator is a short-hand conditional expression:

<?php
$age = 20;
$status = ($age >= 18) ? "Adult" : "Minor";
echo $status; // Outputs: Adult
?>
  • Syntax: (condition) ? value_if_true : value_if_false.

  • Reduces multiple lines of if-else statements.

Common Mistakes and Tips

  • Confusing = (assignment) with == (comparison).

  • Using && vs AND&& has higher precedence.

  • Forgetting to use . for string concatenation.

  • Using === to compare different types – ensures type safety.

  • Always check array key existence before using array operators.

Summary of the Tutorial

  • PHP operators are essential for mathematical, logical, comparison, string, and array operations.

  • Categories: arithmetic, assignment, comparison, increment/decrement, logical, string, array, ternary.

  • Understanding operator precedence ensures accurate calculations.

  • Use string concatenation for building messages and HTML content.

  • Array operators help compare or merge arrays effectively.

  • Ternary operators provide short, readable conditional expressions.

  • Mastering operators is crucial for program logic, decision making, and data manipulation.

Operators are the building blocks of PHP logic, and using them effectively ensures clean, maintainable, and efficient code.


Practice Questions

  1. Create a PHP script that adds two numbers $a = 12 and $b = 8 and prints the result. Include a comment explaining addition.

  2. Write a PHP script that subtracts $b = 5 from $a = 20 and prints the result. Include a comment explaining subtraction.

  3. Create a PHP script that multiplies $x = 7 and $y = 6 and prints the result. Include a comment explaining multiplication.

  4. Write a PHP script that divides $num1 = 15 by $num2 = 4 and prints the result. Include a comment and check for division by zero.

  5. Create a PHP script that calculates the remainder of $num1 = 17 divided by $num2 = 3 using the modulus operator % and prints it. Include a comment.

  6. Write a PHP script that uses the increment operator to increase $count = 10 by 1 and prints the value before and after using both pre-increment and post-increment. Include comments explaining the difference.

  7. Create a PHP script that uses logical operators to evaluate two boolean variables $a = true and $b = false with AND, OR, NOT, and XOR. Include comments explaining each result.

  8. Write a PHP script that concatenates two strings $first = "Hello, " and $second = "World!" using the . operator and prints the result. Include a comment.

  9. Create a PHP script that compares two arrays $array1 = ["x" => 1, "y" => 2] and $array2 = ["y" => 2, "x" => 1] using == and ===, and prints the results. Include comments explaining the difference.

  10. Write a PHP script that uses the ternary operator to check if $age = 18 is greater than or equal to 18 and prints "Adult" or "Minor". Include a comment explaining the ternary operation.


Go Back Top