PHP Operators

Programming

What are The PHP Operators

PHP operators suggest a certain behavior to the PHP processor. The introduction of + symbol, for example, is an operator asking PHP to add two variables or values, while the greater-than > symbol is an operator asking PHP to compare two values. The operators use variables and values to execute operations.

PHP splits the operators into categories below:

  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • Arithmetic operators
  • Assignment operators
  • Conditional operators
PHP Operators

Comparison operators :
The two values are compared using PHP comparison operators. Comparison operators are used in Boolean fashion for comparing two values. Assume that variable V1 contains 30 and the variable V2 contains 40.

Example :

OperatorDescriptionExample
Checks if the left operand value is greater than the right operand value, if yes then the condition becomes valid.(V1 > V2) is not true.
Checks if the left operand value is less than the right operand value, if yes then the condition becomes valid.(V1 < V2) is true.
>=Checks if the left operand value is greater than or equal to the right operand value, if yes then condition becomes true.(V1 >= V2) is not true.
<=Checks if the left operand value is less than or equal to the right operand value, if yes then the condition becomes valid.(V1 <= V2) is true.
!=Checks whether or not the value of two operands is equal, if the values are not equal then the condition becomes valid.(V1 != V2) is true.
==Checks whether or not the value of two operands is equal if yes then condition becomes valid.(V1 == V2) is not true.

Increment/Decrement operators :
The operators of PHP incrementation are used to increase the value of a variable. The PHP decrement operators are used to decrease the value of a variable.

Example :

OperatorNameEffect
++$V1Pre-incrementAdds $V1 to one, then returns $V1
– – $V1Pre-decrementSubtracts $V1 by one then returns $V1
$V1++Post-incrementReturns $V1, then Adds $V1 by one
$V1 – –Post-decrementReturns $V1, then Subtracts$V1 by one

Logical operators :
The logical operators in PHP are used for combining conditional statements. Assume that variable V1 contains 30 and the variable V2 contains 40.

Example :

OperatorDescriptionExample
!Called Operator Logical NOT. Using to reverse their operand’s logical condition. When a condition is valid then the operator Logical NOT will make it false.!(V1 && V2) is false.
||Called Operator Logic OR. If either operand is non-zero then condition becomes valid.(V1 || V2) is true.
andNamed Operator Logical AND. If the operands are both true then the condition is valid.(V1 and V2) is true.
orCalled Operator Logic OR. If either operand is non-zero then condition becomes valid.(V1 or V2) is true.
&&Named Operator Logical AND. If both operands are non-zero then this is true condition.(V1 && V2) is true.

Arithmetic operators :
The PHP arithmetic operators are used for common arithmetic operations such as addition, subtraction, multiplication, etc with numeric values. Assume that variable V1 contains 10 and the variable V2 contains 20.

Example :

OperatorDescriptionExample
+Two operands are addedV1 + V2 will give 30
Subtracts from the first the second operandV1 – V2 will give -10
*Multiply the twoV1 * V2 will give 200
/Divide the twoV2 / V1 will give 2
%Operator module and the remainder after an integer divisionV2 % V1 will give 0

Assignment operators :
The PHP assignment operators are used for writing a value to a variable with numeric values. The operator of a base assignment in PHP is =. That means the left operand is set to the assignment expression value on the right.

Example :

OperatorDescriptionExample
=Simple assignment operator, Assign values from right to left operandV3 = V1 + V2 will assign value of V1 + V2 into V3
+=Add AND assignment operator to the left operand and assign the output to the left operandV3 += V1 is equivalent to V3 = V3 + V1
-=Subtract the operator from the left operand and allocate the output to the left operandV3 -= V1 is equivalent to V3 = V3 – V1
*=Multiply AND assignment operator, multiply operand right with operand left and assign the result to operand leftV3 *= V1 is equivalent to V3 = V3 * V1
/=Divide AND assignment operator, divide operand left with operand right and assign the product to operand leftV3 /= V1 is equivalent to V3 = V3 / V1
%=Modulus AND assignment operator, it takes two operands for modulus and assigns the result to the left operandV3 %= V1 is equivalent to V3 = V3 % V1

Conditional Operators :
Another operator is called aconditional operator. This first evaluates an expression for a true or false value and then executes one of the two statements that are given depending on the evaluation result.

Example :

OperatorDescriptionExample
? :Conditional ExpressionIf Condition is true ? Then value V1 : Otherwise value V2