1. Home
  2. Docs
  3. Web Technology II
  4. Introduction
  5. PHP Operators

PHP Operators

Operators in PHP are special symbols or keywords that perform operations on variables and values.

‣ They can be classified into several types:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Incrementing/Decrementing Operators
  • Logical Operators
  • String Operators
  • Array Operators
  • Type Operators

Arithmetic operators are used to perform common arithmetic operations such as addition, subtraction, multiplication, and division.

Examples:

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

echo $x + $y; // Output: 15 (Addition)
echo $x - $y; // Output: 5 (Subtraction)
echo $x * $y; // Output: 50 (Multiplication)
echo $x / $y; // Output: 2 (Division)
echo $x % $y; // Output: 0 (Modulus)

?>

Assignment operators are used to assign values to variables. They can also perform operations such as addition or multiplication during the assignment.

Examples:

<?php
$x = 10; // Assigns 10 to $x
$y = 5;
$x += $y; // Equivalent to $x = $x + $y; Output: 15
$x -= $y; // Equivalent to $x = $x - $y; Output: 10
$x *= $y; // Equivalent to $x = $x * $y; Output: 50
$x /= $y; // Equivalent to $x = $x / $y; Output: 10
$x %= $y; // Equivalent to $x = $x % $y; Output: 0

?>

Comparison operators are used to compare two values. They return a boolean value (true or false) based on the comparison.

Examples:

<?php
$x = 10;
$y = 5;
var_dump($x == $y); // Output: bool(false) (Equal)
var_dump($x != $y); // Output: bool(true) (Not equal)
var_dump($x > $y); // Output: bool(true) (Greater than)
var_dump($x < $y); // Output: bool(false) (Less than) var_dump($x >= $y); // Output: bool(true) (Greater than or equal to)
var_dump($x <= $y); // Output: bool(false) (Less than or equal to)

?>

Incrementing and decrementing operators are used to increase or decrease the value of a variable by one.

Examples:

<?php
$x = 10;
echo ++$x; // Output: 11 (Pre-increment)
echo $x++; // Output: 11 (Post-increment)
echo --$x; // Output: 9 (Pre-decrement)
echo $x--; // Output: 9 (Post-decrement)

?>

Logical operators are used to combine conditional statements. They include AND, OR, and NOT.

  • Logical AND (&& )
    • The AND operator is used to combine two or more conditions, and it returns TRUE if both conditions are true and, if either condition is false, the result will be false.
$a = 10;
$b = 20;
if ($a > 5 && $b > 15) {
    echo "Both conditions are true."; // This will be printed because both conditions are true.
}
  • Logical OR (|| )
    • The OR operator is used to combine two or more conditions, and it returns TRUE if at least one condition is true.
    • If at least one condition is true, the result is true; if both are false, the result will be false.
$a = 10;
$b = 5;
if ($a > 5 || $b > 15) {
    echo "At least one condition is true."; // This will be printed because the first condition is true.
}
  • Logical NOT (!)
    • The NOT operator is used to reverse the boolean value of an expression. If the condition is true, it makes it false, and if it is false, it makes it true.
$a = 10;
if (!($a > 20)) {
    echo "Condition is false."; // This will be printed because $a is not greater than 20, so NOT makes it false.
}

Examples:

<?php
$x = true;
$y = false;
var_dump($x && $y); // Output: bool(false) (AND)
var_dump($x || $y); // Output: bool(true) (OR)
var_dump(!$x); // Output: bool(false) (NOT)

?>

String operators are used to manipulate string values. The concatenation operator (.) joins two strings.

Examples:

<?php
$str1 = "Hello";
$str2 = "World";
echo $str1 . " " . $str2; // Output: Hello World (Concatenation)

?>

Array operators are used to compare arrays and combine them. They include union, equality, and identity.

Examples:

<?php
$array1 = array("a" => "apple", "b" => "banana");
$array2 = array("c" => "cherry", "d" => "date");
$result = $array1 + $array2; // Union
var_dump($result); // Output: array("a" => "apple", "b" => "banana", "c" => "cherry", "d" => "date")
var_dump($array1 == $array2); // Output: bool(false) (Equality)
var_dump($array1 === $array2); // Output: bool(false) (Identity)

?>

Type operators are used to check the type of a variable or cast a variable to a specific type. The instanceof operator is used to determine if an object is an instance of a specific class.

Examples:

<?php
class MyClass {}
$instance = new MyClass();
var_dump($instance instanceof MyClass); // Output: bool(true) (instanceof)

?>

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *