Lesson 2 : - Operators in PHP

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.

OperatorDescriptionExampleOutput
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division10 / 25
%Modulus5 % 21
**Exponentiation5 ** 225
$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).

OperatorMeaningExampleResult
==Equal$a == $bfalse
!=Not Equal$a != $btrue
===Identical$a === $bfalse
!==Not Identical$a !== $btrue
>Greater Than$a > $btrue
<Less Than$a < $bfalse

🔐 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




Post a Comment

0 Comments