HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

PHP Operators

Operators are used to perform operations on variables and values. In PHP, operators are grouped into several categories based on the type of operation they perform—from simple math to complex logical evaluations.


Operator Groups

To help you learn, PHP operators are divided into the following categories:

Arithmetic

Used with numeric values to perform common mathematical operations, like addition or subtraction.

Assignment

Used with numeric values to write a value to a variable (e.g., = or +=).

Comparison

Used to compare two values (number or string) and return a boolean result.

Logical

Used to combine conditional statements (e.g., and, or, not).


Basic Syntax

Most operators work with two values (operands). For example, in the expression 5 + 10, 5 and 10 are operands, and + is the operator.

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

    echo $x + $y; // Math operator
    echo ($x == $y); // Comparison operator
?>

Specialized Operators

Beyond math and logic, PHP includes several specialized operators that make your code more concise:

  • String Operators: Specifically for joining strings (. and .=).
  • Array Operators: For comparing and merging arrays.
  • Conditional Assignment: Such as the Ternary operator (? :) and the Null Coalescing operator (??).
Important: Operands can be literal values (like 5) or variables (like $x). PHP will handle the underlying data types automatically during the operation.
Learning Path: We will explore each of these operator groups in detail in the following lessons. Mastering these is the key to writing functional PHP logic.

Key Points to Remember

  • Operators are symbols used to manipulate variables and values.
  • There are seven main groups of operators in PHP.
  • Arithmetic handles math; **Comparison** handles logic (true/false).
  • Assignment operators store values in variables.
  • Operators like ?? (Null Coalescing) are powerful modern features of PHP.