PHP Operator Types

PHP supports a variety of operators that can be used to perform arithmetic, logical, and comparison operations.

  • Arithmetic operators are used to perform mathematical operations on numbers. The following table lists the arithmetic operators in PHP:
OperatorDescription
  • | Addition
  • | Subtraction
  • | Multiplication / | Division % | Modulo ** | Exponentiation

  • Logical operators are used to perform logical operations on Boolean values. The following table lists the logical operators in PHP:

OperatorDescription
&&Logical AND
Logical OR
!Logical NOT

  • Comparison operators are used to compare values. The following table lists the comparison operators in PHP:
OperatorDescription
==Equal to
!=Not equal to

| Greater than < | Less than = | Greater than or equal to <= | Less than or equal to

  • Assignment operators are used to assign values to variables. The following table lists the assignment operators in PHP:
OperatorDescription
=Simple assignment
+=Addition assignment
-=Subtraction assignment
*=Multiplication assignment
/=Division assignment
%=Modulo assignment
**=Exponentiation assignment
  • Increment and decrement operators are used to increment or decrement the value of a variable. The following table lists the increment and decrement operators in PHP:
OperatorDescription
++Increment
--Decrement
  • Ternary operator is a conditional operator that can be used to evaluate a Boolean expression and return one of two values, depending on the result of the expression. The syntax for the ternary operator is as follows:
Code snippet
condition ? value_if_true : value_if_false;

The condition expression is evaluated first. If the condition is true, the value_if_true expression is evaluated and returned. If the condition is false, the value_if_false expression is evaluated and returned.

For example, the following code uses the ternary operator to calculate the maximum of two numbers:

Code snippet
$a = 10;
$b = 20;

$max = $a > $b ? $a : $b;

The max variable will now contain the value 20, because 20 is greater than 10.

No comments:

Post a Comment