PHP Operator

Posted On: March 4, 2016

Operator could be given using term 4 + 7 is equal to 11. Here 4 and 7 are called operands and + is called operator.

PHP supports subsequent type of operators:-
• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Conditional (or ternary) Operators
Arithmetic Operators:-
These operators are used with numeric values to execute universal arithmetical operations, such as addition, subtraction etc.

Example:-

OperatorDescriptionExample
+Adds two operands5 + 1 will give 6
Subtracts second operand from the first5 – 1 will give 4
*Multiply both operands4 * 5 will give 20
/Divide numerator by de-numerator5 / 1 will give 5
%Modulus Operator and remainder of after an integer division5% 1 will give 0
++Increment operator, increases integer value by one5++ will give 6
Decrement operator, decreases integer value by one5– will give 4

Comparison Operators:-
These operators are used for comparison.

Example:-

OperatorDescriptionExample
==Checks if the value of two operands is equal or not, if yes then condition becomes true.(5 == 1) is not true.
!=Checks if the value of two operands is equal or not, if values are not equal then condition becomes true.(5 != 1) is true.
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(1> 5) is not true.
<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(1 < 5) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(1 >= 5) is not true.
<=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(1 <= 1) is true.
Also Read :   Magento 2 : Programmatically creating Magento product

Logical Operators:-
These operators are used for logical functions.

Example:-

OperatorDescriptionExample
andCalled Logical AND operator. If both the operands are true then condition becomes true.(K and Z) is true.
orCalled Logical OR Operator. If any of the two operands are non zero then condition becomes true.(K or Z) is true.
&&Called Logical AND operator. If both the operands are non zero then condition becomes true.(K && Z) is true.
||Called Logical OR Operator. If any of the two operands are non zero then condition becomes true.(K || Z) is true.
!Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.!(K && Z) is false.

Assignment Operators:-
There are subsequent assignment operators.

Example:-

OperatorDescriptionExample
=Simple assignment operator, Assigns values from right side operands to left side operandK = L + M will assign value of L + M into K
+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operandK += L is equivalent to K = K + L
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operandK-= L is equivalent to K = K – L
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operandK*= L is equivalent to K = K * L
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operandK/= L is equivalent to K = K / L
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operandK %= L is equivalent to K = K % L
Also Read :   PHP Magic Methods

Conditional Operator:-
This operator evaluates an expression for a true or false value and then executes one of the two given declarations depending upon the result of the assessment.

Example:-

OperatorDescriptionExample
? :Conditional ExpressionIf Condition is true ? Then value K : Otherwise value M

 

X
Subscribe to our newsletter
Subscribe to our newsletter today to receive updates on the latest news and offers.
100% Privacy. No spam guaranteed
webe tutorial
Menu