1. Arithmetic Operators:
Used for arithmetic operations such as addition, subtraction, multiplication, division, modulus, and exponentiation.
php$a = 10;
$b = 5;
$sum = $a + $b; // Addition
$difference = $a - $b; // Subtraction
$product = $a * $b; // Multiplication
$quotient = $a / $b; // Division
$remainder = $a % $b; // Modulus (remainder)
$exponentiation = $a ** $b; // Exponentiation
2. Assignment Operators:
Used to assign values to variables.
php$x = 10; // Assigns the value 10 to $x
$y = 5;
$y += 2; // Equivalent to $y = $y + 2;
$y -= 3; // Equivalent to $y = $y - 3;
$y *= 4; // Equivalent to $y = $y * 4;
// Similarly, /=, %= for division, modulus operations
3. Comparison Operators:
Used to compare values and return a boolean (true or false) result.
php$num1 = 10;
$num2 = 5;
$result1 = ($num1 == $num2); // Equal to
$result2 = ($num1 != $num2); // Not equal to
$result3 = ($num1 > $num2); // Greater than
$result4 = ($num1 < $num2); // Less than
$result5 = ($num1 >= $num2); // Greater than or equal to
$result6 = ($num1 <= $num2); // Less than or equal to
4. Logical Operators:
Used to combine conditional statements.
php$x = true;
$y = false;
$result1 = ($x && $y); // Logical AND
$result2 = ($x || $y); // Logical OR
$result3 = !$x; // Logical NOT
5. String Operators:
Used for concatenating strings.
php$str1 = "Hello, ";
$str2 = "world!";
$greeting = $str1 . $str2; // Concatenation using .
6. Increment/Decrement Operators:
Used to increment or decrement numeric values by one.
php$num = 5;
$num++; // Increment by 1
$num--; // Decrement by 1
Understanding and utilizing operators is essential for performing various tasks in PHP programming, from mathematical operations to making logical decisions and concatenating strings. These operators play a crucial role in building complex functionalities within PHP scripts and applications.
Thanks for learning