PHP operators are symbols or keywords used to perform operations on variables and values. They are used to manipulate data and perform calculations in PHP programs. PHP supports a wide range of operators, including arithmetic, assignment, comparison, logical, and bitwise operators.
Arithmetic operators are used to perform mathematical calculations on numeric values. The following are the arithmetic operators supported by PHP:
The following code example demonstrates the use of arithmetic operators:
<?php $x = 10; $y = 5; echo $x + $y; // Output: 15 echo $x - $y; // Output: 5 echo $x * $y; // Output: 50 echo $x / $y; // Output: 2 echo $x % $y; // Output: 0 echo $x ** $y; // Output: 100000 ?>
Assignment operators are used to assign values to variables. The following are the assignment operators supported by PHP:
The following code example demonstrates the use of assignment operators:
<?php $x = 10; $x += 5; // Equivalent to: $x = $x + 5; echo $x; // Output: 15 $x -= 5; // Equivalent to: $x = $x - 5; echo $x; // Output: 10 $x *= 2; // Equivalent to: $x = $x * 2; echo $x; // Output: 20 $x /= 2; // Equivalent to: $x = $x / 2; echo $x; // Output: 10 $x %= 3; // Equivalent to: $x = $x % 3; echo $x; // Output: 1 ?>
Comparison operators are used to compare values. They return a boolean value (true or false) depending on whether the comparison is true or false. The following are the comparison operators supported by PHP:
The following code example demonstrates the use of comparison operators:
<?php $x = 10; $y = 5; var_dump($x == $y); // Output: bool(false) var_dump($x != $y); // Output: bool(true) var_dump($x < $y); // Output: bool(false) var_dump($x > $y); // Output: bool(true) var_dump($x <= $y); // Output: bool(false) var_dump($x >= $y); // Output: bool(true) var_dump($x <=> $y); // Output: int(1) ?>
Logical operators are used to combine multiple conditions. They return a boolean value (true or false) depending on whether the conditions are true or false. The following are the logical operators supported by PHP:
The following code example demonstrates the use of logical operators:
<?php $x = 10; $y = 5; var_dump($x > 5 and $y < 10); // Output: bool(true) var_dump($x > 5 or $y > 10); // Output: bool(true) var_dump($x > 5 xor $y > 10); // Output: bool(true) var_dump(!($x > 5)); // Output: bool(false) var_dump($x > 5 && $y < 10); // Output: bool(true) var_dump($x > 5 || $y > 10); // Output: bool(true) ?>
Bitwise operators are used to perform operations on binary numbers. They are used to manipulate individual bits in a binary number. The following are the bitwise operators supported by PHP:
The following code example demonstrates the use of bitwise operators:
<?php $x = 10; // Binary: 1010 $y = 5; // Binary: 0101 echo $x & $y; // Output: 0 (Binary: 0000) echo $x | $y; // Output: 15 (Binary: 1111) echo $x ^ $y; // Output: 15 (Binary: 1111) echo ~$x; // Output: -11 (Binary: 11111111111111111111111111110101) echo $x << 1; // Output: 20 (Binary: 10100) echo $x >> 1; // Output: 5 (Binary: 0101) ?>
These are the different types of operators supported by PHP. They are used extensively in PHP programs to perform various operations on variables and values.