Operators in PHP
Operators are symbols that let PHP perform operations on variables and values. They are used for mathematical calculations, comparisons, logical decisions, string operations, and more.
๐ข 1. Arithmetic Operators
Used for mathematical calculations like addition, subtraction, etc.
| Operator | Description | Example | Output |
|---|---|---|---|
| + | Addition | 5 + 2 | 7 |
| - | Subtraction | 5 - 2 | 3 |
| * | Multiplication | 5 * 2 | 10 |
| / | Division | 10 / 2 | 5 |
| % | Modulus | 5 % 2 | 1 |
| ** | Exponentiation | 5 ** 2 | 25 |
$a = 5;
$b = 2;
echo $a + $b; // 7
echo $a % $b; // 1
๐งพ 2. Assignment Operators
Used to assign values and perform compound assignments.
$a = 5;
$a += 2; // $a becomes 7
$a *= 2; // $a becomes 14
๐ง 3. Comparison Operators
Used to compare values (mostly used in conditions).
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal | $a == $b | false |
| != | Not Equal | $a != $b | true |
| === | Identical | $a === $b | false |
| !== | Not Identical | $a !== $b | true |
| > | Greater Than | $a > $b | true |
| < | Less Than | $a < $b | false |
๐ 4. Logical Operators
Used to combine conditions (true/false logic).
$a = true;
$b = false;
echo $a && $b; // false
echo $a || $b; // true
echo !$a; // false
๐งต 5. String Operators
Used to combine strings (concatenation).
$a = "Hello";
$b = " World";
echo $a . $b; // Hello World
$a .= $b; // $a = "Hello World"
๐งบ 6. Array Operators
Used to work with and compare arrays.
$a = ["apple", "banana"];
$b = ["banana", "orange"];
print_r($a + $b); // Union of arrays
⚡ 7. Bitwise Operators
Used to perform operations on binary numbers.
$a = 5; // 0101
$b = 3; // 0011
echo $a & $b; // 1
echo $a | $b; // 7
✅ Summary:
- Arithmetic: for calculations
- Assignment: assign and update values
- Comparison: test conditions
- Logical: combine conditions
- String: work with text
- Array: combine/compare arrays
- Bitwise: advanced binary operations

No comments:
Post a Comment