PHP Operators

Posted by

PHP operators work on the variable or value which can be one or more. We can say that it takes some values or variables, performs the operation on those values, and gives the result. The operator can be “+”, “-“, “%”, etc.

For example:

5 + 6 = 11

In the above example, 5 and 6 are the values, “+” is the operator and gives the result 11.

PHP operators are divide into the following categories:

Now let us learn in detail about each of these operators:

Arithmetic Operators:

Arithmetic operators use simple math operations such as addition, subtraction, multiplication, etc.

Following is a full list of arithmetic operators:

Addition(+):

It works on more than one value and variable. Let’s take an example to understand.

Example:

Output:

Subtraction(-):

It is used to find the difference between two values or variables.

Example:

Output:

Multiplication(*):

Multiplication operator is used to multiplying two values or variables.

Example:

Output:

Division(/):

It is used for dividing the values or variables.

Example:

Output:

Modulus(%):

Modulus operator gives the remainder if we divide the two values or variables.

Example:

Output:

Exponentiation(**):

It is used to raise the power.

Example:

Output:

Let’s take more examples:

Assignment operators:

Assignment operators(=) use a variable to assign value. The Assignment Operator’s left side operand is a variable, and the assignment operator’s right side operand is a value.

Operator Name Syntax Same As
= Assign $a = $b $a = $b
+= Addition and assign $a += $b $a = $a + $b
-= Subtraction and assign $a -= $b $a = $a – $b
*= Multiply and assign $a *= $b $a = $a * $b
/= Divide and assign quotient $a /= $b $a = $a / $b
%= Divide and assign modulus $a %= $b $a = $a % $b

Example:

Comparison operators:

Comparison operators are required to check for certain differences or similarities between the values and variables. It works like booleans, giving back either true or false value.

Comparison operators are: >, <, >=, <=, ===, and !==.

Less than(<):

If the value on the left is less than the value on the right, it returns true, otherwise, it returns false.

Example:

Greater than(>):

If the value on the left is greater than the value on the right, it returns true, otherwise, it returns false.

Example:

Less than or equal to (<=):

If the left-hand value is less than or equal to the right-hand value, it returns true, otherwise, it returns false.

Greater than or equal to (>=):

If the left-hand value is greater than or equal to the right-hand value, it returns true, otherwise, it returns false.

Equal to (==):

If the left-hand value is equal to the right-hand value, it returns true otherwise it returns false.

Not equal to (!=):

If the left-hand value is not equal to right-hand value, it returns true otherwise, it returns false.

Identical(===):

If the left-hand value is equal and the same data type to the right-hand value, it returns true otherwise it returns false.

Not Identical(!==):

If the left-hand value is not equal to the right-hand value or data type is different then it returns true otherwise it returns false.

Increment/Decrement operators:

It is used for increment or decrements the value of variables. The following are the increment or decrement operators:

Pre-increment(++$a):

It increments the value of variable $a first and return $a.

Post-increment($a++):

It returns the value of variable first and then returns $a.

Pre-decrement(–$a):

It decrements the value of variable $a first and returns $a

Post-decrement($a–):

It decrements the value of variable $a first and return $a.

Logical operators:

Following are the Logical operator which are used to check the conditions:

AND(&&): It returns true if all the condition is true otherwise returns false.

OR(||): It returns true if any of them is true otherwise returns false.

NOT(!): It returns true if the variable is not true.

String operators:

The following are the string operator:

Concatenation(.):

It is used to combine two strings.

Example:

Concatenation and assignment:

It combines the string and assigns.

Example:

Conditional assignment operators:

The following are the Conditional assignment operators:

Ternary(?:)

Example:

Null coalescing(??): It is a new operator introduced in PHP 7. It returns the first operand if it is set otherwise retund the second operand.

Example:

In the above example, the value of $a is 10 so the output will be shown 10.

In this example, the value of $a is not set so the output will be shown null.

 

If you wish to share more information or have a query about the topic PHP Operators, please write comments.

Leave a Reply

Your email address will not be published. Required fields are marked *