techmore.in

PHP - Operators

PHP supports a wide range of operators that allow you to perform various operations on variables and values. Operators in PHP can be categorized into several types based on their functionality. Here's an overview of the main types of operators in PHP:

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations:

  • Addition (+): Adds two operands together.
  • Subtraction (-): Subtracts the right operand from the left operand.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the left operand by the right operand.
  • Modulus (%): Returns the remainder of the division of the left operand by the right operand.
  • Increment (++): Increases the value of the operand by 1.
  • Decrement (--): Decreases the value of the operand by 1.

Example:

php
<?php $a = 10; $b = 3; echo $a + $b; // Output: 13 echo $a - $b; // Output: 7 echo $a * $b; // Output: 30 echo $a / $b; // Output: 3.333... echo $a % $b; // Output: 1 (remainder of 10 divided by 3) ?>

Assignment Operators

Assignment operators are used to assign values to variables:

  • Assignment (=): Assigns the value of the right operand to the left operand.
  • Addition assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
  • Subtraction assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
  • Multiplication assignment (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.
  • Division assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
  • Modulus assignment (%=): Computes the modulus of the left operand with the right operand and assigns the result to the left operand.

Example:

php
<?php $x = 10; $y = 5; $x += $y; // Equivalent to: $x = $x + $y; echo $x; // Output: 15 $x -= $y; // Equivalent to: $x = $x - $y; echo $x; // Output: 10 ?>

Comparison Operators

Comparison operators are used to compare two values:

  • Equal to (==): Returns true if the operands are equal.
  • Identical (===): Returns true if the operands are equal and of the same type.
  • Not equal to (!= or <>): Returns true if the operands are not equal.
  • Not identical (!==): Returns true if the operands are not equal or not of the same type.
  • Greater than (>): Returns true if the left operand is greater than the right operand.
  • Less than (<): Returns true if the left operand is less than the right operand.
  • Greater than or equal to (>=): Returns true if the left operand is greater than or equal to the right operand.
  • Less than or equal to (<=): Returns true if the left operand is less than or equal to the right operand.

Example:

php
<?php $a = 10; $b = 5; var_dump($a == $b); // Output: bool(false) var_dump($a > $b); // Output: bool(true) ?>

Logical Operators

Logical operators are used to combine conditional statements:

  • Logical AND (&& or and): Returns true if both operands are true.
  • Logical OR (|| or or): Returns true if either operand is true.
  • Logical NOT (! or not): Returns true if the operand is false and vice versa.

Example:

php
<?php $a = true; $b = false; var_dump($a && $b); // Output: bool(false) var_dump($a || $b); // Output: bool(true) ?>

Other Operators

PHP also supports other types of operators such as:

  • Concatenation (.): Concatenates two strings together.
  • Ternary (?:): Provides a shorthand way of writing if-else statements.
  • Null Coalescing (??): Returns the first operand if it exists and is not null, otherwise returns the second operand.

Example:

php
<?php $name = "John"; echo "Hello " . $name; // Output: Hello John $result = ($a > $b) ? "Greater" : "Lesser"; // Ternary operator echo $result; // Output depends on the comparison of $a and $b $var = $a ?? "default"; // Null coalescing operator echo $var; // Output depends on whether $a is null or not ?>