Operators in PHP – Free PHP Tutorials

Operators in PHP define the type of operation to be performed on the operands i.e. values and variables. Operators are of great importance for any programming language. It makes problem-solving very easy.

In PHP we have the following set of standard operators:

  1. Increment and Decrement
  2. Arithmetic
  3. Relational/Comparison
  4. String
  5. Assignment
  6.  Array
  7. Logical

INCREMENT AND DECREMENT 

In programming terms, we have the following set of increment and decrement operators

$x++ :- This specifies post-increment operation for addition.

++$x :- This specifies pre-increment operation for addition.

$x-- :- This specifies post-increment operation for subtraction.

--$x :- This specifies pre-increment operation for subtraction.


ARITHMETIC 

These are the basic arithmetic operators that we have been studying in maths so far i.ie.

+, -, *, /, %

$x+$y => adds the two operands

$x-$y => subtratcs the two operands

$x*$y => multiplies the two operands

$x/$y => divides the two operands

$x%$y => calculates the remainder


RELATIONAL 

In relational or also sometimes referred as comparison operators are as follows:

<, >, >=, <=, ==, !=, ===, !==

$x<$y => checks if y is greater than x

$x>$y => checks if y is less than x

$x<=$y =>checks if y is greater  than or equal to x

$x>=$y =>checks if  y is less than or equal to x

$x!=$y => checks if x is not equal to y

$x!==$y => checks if x is not identical to y

$x==$y => checks if x is equal equal to y

$x===$y => checks if x is identical to y


STRING 

. is the conacatenation operator which is used to add or conactenate two strings

.= is conacatenating assignement operator which appends the value of two string and stores it in the assigned variable.

<?php

$x="hello";

$y=$x." world"; // conactenating the string variable using . operator

$a="hello ";

$a.="world";
echo "".$y;
echo "".$a;

?>

The output will be:

Hello world
Hello world

 


ARRAY 

The opeators defined under this section are used particularly on arrays. The operators are as follows:

+=> defines the union operation between two arrays.

== => check for the equality of the two arrays.

=== => checks for if the key-value pairs are in the same order or not in the given two arrays.

!= => checks if two arrays are not equal. We can also use <> instead of !=.

!== => checks if the two arrays are not identical.


ASSIGNMENT 

The assignment operators are most commonly used in combination with the arithmetic operators.T he following are the most commonly used combinations which reduce the expression length too.

$x=$y => this operation assigns the value of y to x.

$x+=$y => this operation computes the sum and assigns the result to x.

$x-=$y => this operation computes the difference and assigns the result to x.

$x*=$y => this operation computes the product and assigns the result to x.

$x/=$y => this operation computes the quotient and assigns the result to x.

$x%=$y => this operation computes the remainder and assigns the result to x.


LOGICAL 

These operators are used for logical computations of result from a combination of expressions under a specific condition. The following are the standard logical operators:

$x and $y => it returns true if both of the expressions are true otherwise false.

$x or $y => it returns true if one of the expressions is true otherwise false.

$x xor $y => it returns true if either of them is true otherwise false.

$x && $y => it returns true if both of the expressions are true otherwise false.

$x || $y => it returns true if one of the expressions is true otherwise false.

!/not $x => it returns false if $x is true.

If you have any doubts, mention it in the comment section below.

Leave a Comment

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