PHP Operators

PHP Operators

  • PHP
  • 15 mins read

The operator is a symbol that will take one or more operands and operate on them and produce a result. PHP provides the following operators:

• Arithmetic operators
• Increment/Decrement operators
• Comparison operators
• Logical operators
• Assignment operators
• String operators
• Array operators

Arithmetic operators

Arithmetic operators are used to performing arithmetical operations such as addition, subtraction, multiplication, division, etc. These arithmetical operations are applicable only for numeric values. The PHP Arithmetic operators are:

i. Addition operator(+)
ii. Subtraction operator(-)
iii. Multiplication operator(*)
iv. Division operator(/)
v. Modulus operator(%)
vi. Exponentiation(**)

Program on Addition operator

<html>
<body>
<?php
$a=10;
$b=20;
$c=$a+$b;
echo $c;
?>
</html>
</body>

Output: 30(addition of two variables $a and $b)

Program on Subtraction operator

<html>
<body>
<?php
$a=100;
$b=20;
$c=$a-$b;
echo $c;
?>
</html>
</body>

Output:80(subtraction of two variables $a and $b)

Program on Multiplication operator

<html>
<body>
<?php
$a=10;
$b=20;
$c=$a*$b;
echo $c;
?>
</html>
</body>

Output:200(multiplication of two variables $a and $b)

Program on Division operator

<html>
<body>
<?php
$a=90;
$b=20;
$c=$a/$b;
echo $c;
?>
</html>
</body>

Output:4.5(division of two variables $a and $b)

Program on Modulus operator

<html>
<body>
<?php
$a=90;
$b=20;
$c=$a%$b;
echo $c;
?>
</html>
</body>

Output:10(remainder of two variables $a and $b.Modulus operator will return the remainder)

Program on Exponentiation operator

<html>
<body>
<?php
$a=9;
$b=2;
$c=$a**$b;
echo $c;
?>
</html>
</body>

Output:81(raising $a to the $b’th power)

Increment/decrement operators

Increment operator:

This operator is used to increase the value of a variable by 1.

Decrement operator:

This operator is used to decrease the value of a variable by 1.

i. Increment operator(++)
ii. Decrement operator(--)

Program on Increment operator

<html>
<body>
<?php
$a=10;
$b=++$a;
echo $b;
?>
</body>
</html>

Output:11(Here ++$a is a pre-increment operation. Here firstly the value of $a will be increased by 1 then the result will be assigned to $b)

<html>
<body>
<?php
$a=10;
$b=$a++;
echo $b;
?>
</body>
</html>

Output:10(Here $a++ is a post-increment operation. Here the value of $a will be returned and assigned to $b then the value of $a will be increased by 1.So the output will be 10)

<html>
<body>
<?php
$a=10;
$b=$a++;
echo $a;
?>
</body>
</html>

Output:11(Here $a++ is a post-increment operation. Here the value of $a will be returned and assigned to $b then the value of $a will be Increased by 1. So the output will be 11)

Program on Decrement operator

<html>
<body>
<?php
$a=10;
$b=--$a;
echo $b;
?>
</body>
</html>

Output:9(Here --$a is a pre-decrement operation. Here firstly the value of $a will be decreased by 1 then the result will be assigned to $b)

<html>
<body>
<?php
$a=10;
$b=$a--;
echo $b;
?>
</body>
</html>

Output:10(Here $a-- is a post-decrement operation. Here the value of $a will be returned and assigned to $b then the value of $a will be decreased by 1.So the output will be 10)

<html>
<body>
<?php
$a=10;
$b=$a--;//here the value of $a will be returned and assigned to $b
echo $a;
?>
</body>
</html>

Output:9(Here $a-- is a post-decrement operation. Here the value of $a will be returned and assigned to $b then the value of $a will be decreased by 1.So the output will be 9)

Comparison operator

This operator is used to compare between two values. The values can be numbers and strings. The comparison operators are:

i. Greater than operator(>)
ii. Less than operator(<)
iii. Greater than or equal to operator(>=)
iv. Less than or equal to operator(<=)
v. Equal to operator(==)
vi. Not equal to operator(<>,!=)
vii. Identical operator(===)
viii. Not identical operator(!==)

Program on Greater than operator

<html>
<body>
<?php
$a=100;
$b=90;
$c=$a>$b;
echo $c;
?>
</body>
</html>

Output:0( here $a is greater than $b so $a>$b will return true as 1 and 1 will be assigned to c so here the output is 1. Actually $a>$b will return true as 1 if $a is greater than $b)

Program on Less than operator

<html>
<body>
<?php
$a=10;
$b=90;
$c=$a<$b;
echo $c;
?>
</body>
</html>

Output:1( Here $a is smaller than $b so $a<$b will return true as 1 and 1 will be assigned to c so here the output is 1.Actually $a<$b will return true as 1 if $a is smaller than $b)

Program on Greater than or equal to operator

<html>
<body>
<?php
$a=10;
$b=10;
$d=$a>=$b;
echo $d;
?>
</body>
</html>

Output:1( $a>=$b will return true as 1 if $a is greater than or equal to $b.Here $a>=$b will return 1 and 1 will be assigned to d and so the output will be 1)

Program on Less than or equal to operator

<html>
<body>
<?php
$a=10;
$b=10;
$d=$a<=$b;
echo $d;
?>
</body>
</html>

Output:1( $a<=$b will return true as 1 if $a is smaller than or equal to $b .Here $a<=$b will return 1 and 1 will be assigned to d and so the output is 1)

Program on Equal to operator

<html>
<body>
<?php
$a=10;
$b=10;
$d=$a==$b;
echo $d;
?>
</body>
</html>

Output:1($a==$b will return true as 1 if $a is equal to $b.Here $a==$b will return 1and 1 will be assigned to d and so the output is 1)

Program on Not equal to operator 1

<html>
<body>
<?php
$a=10;
$b=20;
$d=$a<>$b;
echo $d;
?>
</body>
</html>

Output:1($a<>$b will return true as 1 if $a is not equal to $b.Here $a<>$b will return 1 and 1 will be assigned to d and so the output is 1)

Program on Not equal to operator 2

<html>
<body>
<?php
$a=10;
$b=20;
$d=$a!=$b;
echo $d;
?>
</body>
</html>

Output:1($a<>$b will return true as 1 if $a is not equal to $b.Here $a<>$b will return 1 and 1 will be assigned to d and so the output is 1)

Program on Identical operator

<html>
<body>
<?php
$a=10.5;
$b=10.5;
$d=$a===$b;
echo $d;
?>
</body>
</html>

Output:1($a===$b will return true as 1 if $a is equal to $b and they are of same datatype.Here $a===$b will return 1 and 1 will be assigned to d and so the output is 1)

Program on Not identical operator

<html>
<body>
<?php
$a=10;
$b=11;
$d=$a!==$b;
echo $d;
?>
</body>
</html>

Output:1($a!==$b will return true as 1 if $a is equal to $b or they are of same datatype.Here $a!==$b will return 1 and 1 will be assigned to d and so the output is 1)

Logical operator

These operators are used to combine conditional statements(when we evaluate different conditions through a program, and we make decisions based on these conditions which are evaluated to true or false).
The logical operators are:

• and
• or
• xor
• &&(And)
• ||(Or)
• !(Not)

Programs on Logical And operator

<html>
<body>
<?php
$a=10;
$b=$a>5 and $a<40;
echo $b;
?>
</body>
</html>

Output:1(here when $a=10 is executed then 10 will be assigned to $a.When $b=$a>5 and $a<40 will be executed then ($a>5 and $a<40) will be evaluated first and will return 1 and then 1 will be assigned to $b.So the output will be 1.Actually (expression1 and expression2)will return true if both expressions are evaluated to true)

Programs on Logical Or operator

<html>
<body>
<?php
$a=100;
$b=$a>5 or $a<40;
echo $b;
?>
</body>
</html>

Output:1(here when $a=100 is executed then 100 will be assigned to $a.when $b=$a>5 or $a<40 will be executed then ($a>5 or $a<40) will be evaluated first and will return 1 and then 1 will be assigned to $b.So the output will be 1.Actually (expression1 or expression2)will return true if any of two expressions is evaluated to true)

Programs on Logical And operator (&&)

<html>
<body>
<?php
$a=10;
$b=$a>5 && $a<40;
echo $b;
?>
</body>
</html>

Output:1(here when $a=10 is executed then 10 will be assigned to $a.When $b=$a>5 && $a<40 will be executed then ($a>5 && $a<40) will be evaluated first and will return 1 and then 1 will be assigned to $b.So the output will be 1.Actually (expression1 && expression2)will return true if both expressions are evaluated to true)

Programs on Logical Or operator(||)

<html>
<body>
<?php
$a=10;
$b=$a>5 || $a<40;
echo $b;
?>
</body>
</html>

Output:1(here when $a=10 is executed then 10 will be assigned to $a.When $b=$a>5 || $a<40 will be executed then ($a>5 || $a<40) will be evaluated first and will return 1 and then 1 will be assigned to $b.So the output will be 1.Actually (expression1 || expression2)will return true if any of two expressions is evaluated to true)

Programs on Logical Xor operator(xor)

<html>
<body>
<?php
$a=100;
$b=$a>5 xor $a<40;
echo $b;
?>
</body>
</html>

Output:1(here when $a=100 is executed then 100 will be assigned to $a.When $b=$a>5 xor $a<40 will be executed then ($a>5 xor $a<40) will be evaluated first and will return 1 and then 1 will be assigned to $b.So the output will be 1.Actually (expression1 xor expression2)will return true if either expression1 is evaluated to true or expression2 is evaluated to true)

Programs on Logical Not operator(!)

<html>
<body>
<?php
$a=100;
$b=$a<5;
$c=!$b;
echo $c;
?>
</body>
</html>

Output:1(here when $a=100 is executed then 100 will be assigned to $a.When $b=$a<5 will be executed then ($a<5) will be evaluated first and will specify $b is not true and then $c=!$b will be evaluated and return 1 and then 1 will be assigned to $c.So the output will be 1.Actually (!exp1)will return true if exp1 is not true)

Assignment operator

This operator is used to assign a value to a variable. The assignment operator is:

• The assignment operator(=)\

Additional assignment operator:

• +=
• -=
• *=
• /=
• %=

Programs on Assignment operator

<html>
<body>
<?php
$a=10;
echo $a;
?>
</body>
</html>

Output:10(here when $a=10 is executed than 10 will be assigned to $a.So the output will be 10)
Programs on Assignment operator

<html>
<body>
<?php
$a=10;
$b=90;
$b=$a;
echo $b;
?>
</body>
</html>

Output:10(here when $a=10 is executed then 10 will be assigned to $a then when $b=90 is executed then 90 will be assigned to $b and When $b=$a will be executed then the value of $a will be assigned to $b.So the output will be 10).

Programs on Additional assignment operator(+=)

<html>
<body>
<?php
$a=10;
$b=10;
$b+=$a;
echo $b;
?>
</body>
</html>

Output:20(here when $a=10 is executed then 10 will be assigned to $a then when $b=10 is executed then 10 will be assigned to $b and When $b+=$a will be executed then actually $b+=$a means $b=$b+$a.So 20 will be assigned to $b.So the output will be 20)

Programs on Additional assignment operator(-=)

<html>
<body>
<?php
$a=10;
$b=10;
$b-=$a;
echo $b;
?>
</body>
</html>

Output:0(here when $a=10 is executed then 10 will be assigned to $a then when $b=10 is executed then 10 will be assigned to $b and When $b-=$a will be executed then actually $b-=$a means $b=$b-$a.So 0 will be assigned to $b.So the output will be 0)

Programs on Additional assignment operator(*=)

<html>
<body>
<?php
$a=10;
$b=10;
$b*=$a;
echo $b;
?>
</body>
</html>

Output:100(here when $a=10 is executed then 10 will be assigned to $a then when $b=10 is executed then 10 will be assigned to $b and When $b-=$a will be executed then actually $b*=$a means $b=$b*$a.So 100 will be assigned to $b.So the output will be 100)

Programs on Additional assignment operator(/=)

<html>
<body>
<?php
$a=10;
$b=10;
$b/=$a;
echo $b;
?>
</body>
</html>

Output:1(here when $a=10 is executed then 10 will be assigned to $a then when $b=10 is executed then 10 will be assigned to $b and When $b-=$a will be executed then actually $b/=$a means $b=$b/$a.So 1 will be assigned to $b.So the output will be 1)

Programs on Additional assignment operator(-=)

<html>
<body>
<?php
$a=10;
$b=10;
$b%=$a;
echo $b;
?>
</body>
</html>

Output:0(here when $a=10 is executed then 10 will be assigned to $a then when $b=10 is executed then 10 will be assigned to $b and When $b-=$a will be executed then actually $b%=$a means $b=$b%$a.So 0 will be assigned to $b.So the output will be 0)

String operator

These operators are used to concatenate of strings.

The string operators are:
• Concatenation operator(.)
• Concatenation assignment operator(.=)

Programs on concatenation operator

<html>
<body>
<?php
$a=”john”;
$b=”Fernandes”;
$c=$a.$b;
echo $c;
?>
</body>
</html>

Output:johnFernandes(Here when $a=”john” will be executed then “john” will be assigned to $a, Same “Fernandes” will be assigned to $b.when $c=$a.$b will be executed then $a.$b will be evaluated first, and Two strings will be combined, and the combined string will be assigned to $c.So the output is johnFernandes)

Programs on concatenation assignment operator

<html>
<body>
<?php
$a=”john”;
$b=”Fernandes”;
$b.=$a;
echo $c;
?>
</body>
</html>

Output: Fernandesjohn(Here when $a=”john” will be executed then “john” will be assigned to $a, Same “Fernandes” will be assigned to $b.Actually $b.=$a; Means $b=$b.$a; So the output will be “Fernandesjohn”)

Array operators

These operators are used to compare between two arrays.
The array operators are:
• Union operator(+)
• Equality operator(==)
• Identity operator(===)
• Inequality operator(!=)
• Inequality operator(<>)
• Non-indentity(!==)

Programs on Union operator

<html>
<body>
<?php
$a=array(1=>”john”,2=>”khardah”,3=>”45000”);
$b=array(4=>”Tarak”,5=>”Kolkata”,6=>”95000”);
print_r($a+$b);
?>
</body>
</html>

Output: Array ( [1] => john [2] => khardah [3] => tarak ) (Here $a+$b is a union of $a and $b.print_r() is used to print the array)

Programs on Equality operator

<html>
<body>
<?php
$a=array(1=>”john”,2=>”khardah”,3=>”45000”);
$b=array(1=>”john”,2=>”khardah”,3=>”45000”);
$c=$a==$b;
echo($c);
?>
</body>
</html>

Output:1 (Here $a==$b will return true as 1 if both $a and $b will contain the same key/value pairs)

Programs on Identity operator

<html>
<body>
<?php
$a=array(1=>”john”,2=>”khardah”,3=>”45000”);
$b=array(1=>”john”,2=>”khardah”,3=>”45000”);
$c=$a===$b;
echo($c);
?>
</body>
</html>

Output:1 (Here $a===$b will return true as 1 if both $a and $b contain the same key/value pairs in the same order and of the same types)

Programs on Inequality operator

<html>
<body>
<?php
$a=array(1=>”john”,2=>”khardah”,3=>”45000”);
$b=array(4=>”Tarak”,5=>”Kolkata”,6=>”95000”);
$c=$a!=$b;
echo $c;
?>
</body>
</html>

Output: 1 (Here $a!=$b will return true if $a And $b are not same)

Programs on Inequality operator

<html>
<body>
<?php
$a=array(1=>”john”,2=>”khardah”,3=>”45000”);
$b=array(4=>”Tarak”,5=>”Kolkata”,6=>”95000”);
$c=$a<>$b;
echo $c;
?>
</body>
</html>

Output: 1 (Here $a<>$b will return true if $a And $b are not same)

Programs on Non-identity operator

<html>
<body>
<?php
$a=array(1=>”john”,2=>”khardah”,3=>”45000”);
$b=array(4=>”Tarak”,5=>”Kolkata”,6=>”95000”);
$c=$a!==$b;
echo $c;
?>
</body>
</html>

Output: 1 (Here $a!==$b will return true if $a is not identical to $b)

See also: