How many type of PHP Operators

 There are several types of operators in PHP:

  1. Arithmetic operators: These operators perform basic arithmetic operations, such as addition, subtraction, multiplication, and division.

  2. Comparison operators: These operators compare two values and return a Boolean value (true or false) based on the result of the comparison. Examples include the greater than (>) and less than (<) operators.

  3. Assignment operators: These operators assign a value to a variable. The most basic assignment operator is the equal sign (=), which assigns the value on the right to the variable on the left.

  4. Increment/decrement operators: These operators increase or decrease the value of a variable by 1. The increment operator (++) increases the value of a variable, while the decrement operator (--) decreases it.

  5. Logical operators: These operators perform logical operations, such as AND, OR, and NOT. They are often used in control structures, such as if statements, to evaluate multiple conditions.

  6. String operators: These operators concatenate (combine) two strings. The concatenation operator (.) is used to join two strings together.

  7. Array operators: These operators perform operations on arrays, such as union, intersection, and difference.

  8. Type operators: These operators determine the type of a variable or expression. The type operator (instanceof) checks if an object is an instance of a particular class.

  9. Execution operators: These operators execute a command stored in a string. The execution operator (`) is used to execute a command stored in a string.

  10. Conditional operator: This operator is also known as the ternary operator because it takes three operands. It is a shorthand way of writing an if-else statement and returns a value based on a condition.

  11. Error suppression operator: This operator (@) is used to suppress error messages. It is often used to ignore warning messages that are not critical to the execution of the code.


PHP Operators example

Here are some examples of the different types of operators in PHP:

  1. Arithmetic operators:
$a = 5; $b = 2; echo $a + $b; // 7 echo $a - $b; // 3 echo $a * $b; // 10 echo $a / $b; // 2.5 echo $a % $b; // 1
  1. Comparison operators:
$a = 5; $b = 2; echo $a > $b; // true echo $a < $b; // false echo $a == $b; // false echo $a != $b; // true
  1. Assignment operators:
$a = 5; $b = 2; $a += $b; // $a is now 7 $a -= $b; // $a is now 5 $a *= $b; // $a is now 10 $a /= $b; // $a is now 2.5 $a %= $b; // $a is now 1
  1. Increment/decrement operators:
$a = 5; echo ++$a; // 6 echo $a; // 6 echo --$a; // 5 echo $a; // 5
  1. Logical operators:
$a = true; $b = false; echo $a and $b; // false echo $a or $b; // true echo !$a; // false
  1. String operators:
$a = "Hello"; $b = " World"; echo $a . $b; // "Hello World"
  1. Array operators:
$a = array(1, 2, 3); $b = array(3, 4, 5); $c = $a + $b; // $c is array(1, 2, 3, 4, 5)
  1. Type operators:
class MyClass {} $a = new MyClass; if ($a instanceof MyClass) { echo "a is an instance of MyClass"; }
  1. Execution operators:
$output = `ls -l`; echo $output;
  1. Conditional operator:
$a = 5; $b = 10; $max = $a > $b ? $a : $b; echo $max; // 10
  1. Error suppression operator:
@unlink("non_existent_file");

Popular posts from this blog

Laravel print query