Operators are used to perform operations on variables and values. Variables are simply containers for information. In order to do anything useful with them, you need Operators. Operators are symbols that tell the PHP processor to perform certain actions. For example, the addition (+) symbol is an Operators that tells PHP to add two variables or values, while the greater-than(>) symbol is an Operators that tells PHP to compare two values.
PHP supports more than 50 such Operators , ranging from operators for arithmetical
operations to operators for logical comparison and bitwise calculations.
PHP divides the operators into the following groups
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/ Decrement operators
- Logical operators
- String operators
- Array operators
Arithmetic Operators in PhP
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
Operators | Description |
---|---|
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
** | Exponentiation (Introduced in PHP 5.6) |
Example to use Arithmetic Operator (+) for addition of two numbers
<?php $x=10; $y=5; //addition $sum=$x+$y; echo "sum=".$sum."<br/>"; ?>
Output: sum = 15
Similarly we can use all the remaining Arithmatic Operators.
Assignment Operators in PhP
The PHP assignment operators are used with numeric values to write a value to a variable.
Operators | Description |
---|---|
= | The left operand gets set to the value of the expression on the right |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign quotient |
%= | Divide and assign modulus |
.= | Concatenate and assign (its used only for sting) |
Example to use Add and assign Assignment Operator (+=)
<?php $x = 500; $x+= 500; echo "sum=".$x."<br/>"; ?>
Output: sum=1000
Similarly we can use all the remaining Assignment Operators.
Comparison operators in PhP
The PHP comparison operators are used to compare two values (number or string)
Operators | Description |
---|---|
== | Equal to |
=== | Equal to and of the same type |
!= | Not equal to |
<> | Not equal to |
!== | Not equal to and of the same type |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example to illustrate == and === operators
<?php $x=10; $y=10.0; echo ($x==$y); //it returns true because both the variable contains same value. echo ($x===$y); /*it returns false because === strongly compares. here both variable contain same value i.e 10 but different datatype one is integer and another is float.*/ ?>
Increment/ Decrement operators in PhP
The PHP increment operators are used to increment a variable’s value.
Operators | Description |
---|---|
++$x | Pre-increment |
$x++ | Post-increment |
The PHP decrement operators are used to decrement a variable’s value.
Operators | Description |
---|---|
–$x | Pre-decrement |
$x– | Post-decrement |
Logical operators in PhP
The PHP logical operators are used to combine conditional statements.
Operator | Description |
---|---|
and | And |
or | Or |
xor | Xor |
&& | And |
|| | Or |
! | Not |
Example to use And Operator (&&)
<?php $name="pardeep"; $pass="pardeeppatel"; if($name=="pardeep" && $pass=="pardeeppatel" { header('location:https://www.studywarehouse.com'); } else { echo "Invalid name or password"; } ?>
Output: This program will redirect you on “https://www.studywarehouse.com” page
String operators in PhP
PHP has two operators that are specially designed for strings.
Operator | Description |
---|---|
. | Concatenation |
.= | Concatenation Assignment |
Example to use String Operators
<?php $name="Pardeep"; $lastName="Patel"; echo $name." ".$lastName; $a="Hello"; $a .= " Pardeep!"; echo $a; ?>
Output
Pardeep Patel
Hello Pardeep!
Array operators in PhP
The PHP array operators are used to compare arrays.
Operator | Description |
---|---|
+ | Union |
== | Equality |
=== | Identity |
!= | Inequality |
<> | Inequality |
!== | Non-identity |
Example to use operator in array
<?php $a = array("a" => "apple", "b" => "banana"); $b = array("a" => "pear", "b" => "strawberry", "c" => "cherry"); $c = $a + $b; // Union of $a and $b echo "Union of \$a and \$b : <br />"; var_dump($c); $c = $b + $a; // Union of $b and $a echo "<br />Union of \$b and \$a : <br />"; var_dump($c); ?>
Output
Union of $a and $b :
array(3) { [“a”]=> string(5) “apple” [“b”]=> string(6) “banana” [“c”]=> string(6) “cherry” }
Union of $b and $a :
array(3) { [“a”]=> string(4) “pear” [“b”]=> string(10) “strawberry” [“c”]=> string(6) “cherry” }