Constants in PHP | Types of Constants | Define Constants
What are operators?
Two things are used for some basic arithmetic operations. And they are numbers and signs. Like ‘2+4=6’. So, these numbers and signs are called operands and operators.
Operators are signs (+ – / %).
Operands are numbers. (2, 4, 6)
There are different types of operators.
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Bitwise operators
- String operators
- Array operators
- Conditional (or ternary) Operators
We will discuss each one of them one by one.
Arithmetic operator
Assume variable A has value 2 and variable B has value 4
Operator | Name | Example |
+ | Add | add two numbers. E.g. (2+4) becomes 6 |
– | Subtraction | subtracts two numbers. E.g. (4-2) becomes 2 |
* | Multiply | multiply two numbers. E.g. (2*4) becomes 8 |
/ | Division | divide two numbers. E.g. (4/2) becomes 2 |
% | Modulus (remainder) | give a remainder of two numbers when divided. (4/2) becomes 0 |
++ | Increment operator | adds one value in a variable. E.g. (2++) becomes 3 |
— | Decrement operator | decreases one value in a variable. E.g. (2–) becomes 1 |
Comparison Operators
The comparison operator compares two values and checks if they are according to the condition or not.
If they compare, the result will be True. If they do not, the result will be False.
Assume variable A has value 5 and variable B has a value of 10
Operator | Name | Description | Example |
== | Equal | Checks if the value of two operands are equal or not, if yes then the condition becomes true. | (A == B) is not true. |
!= or <> (use one or both) | Not equal | Checks if the value of two operands are equal or not, if values are not equal then the condition becomes true. | (A != B) is true. |
> | Greater than | Checks if the value of the left operand is greater than the value of the right operand, if yes then the condition becomes true. | (A > B) is not true. |
=== | Identical | Return TRUE if $a is equal to $b, and they are of same data type | $a === $b |
!== | Not identical | Return TRUE if $a is not equal to $b, and they are not of same data type | $a !== $b |
< | Less than | Checks if the value of the left operand is less than the value of the right operand, if yes then the condition becomes true. | (A < B) is true. |
>= | Greater than or equal to | Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then the condition becomes true. | (A >= B) is not true. |
<= | Less than or equal to | Checks if the value of the left operand is less than or equal to the value of the right operand, if yes then the condition becomes true. | (A <= B) is true. |
<=> | Spaceship | Return -1 if $a is less than $b Return 0 if $a is equal $b Return 1 if $a is greater than $b | $a <=>$b |
IF statement
What is the if statement?
When you have to compare two values with each other and you want your answer to be yes or no. Then you will have to use the if statement. So, it is clear that if a statement checks two statements and compares them to whether they are true or not.
If they both match the condition the result will be True. If they do not match the condition the result will be False.
So, we will check them using comparison operators which we discussed earlier.
Syntax of IF statement.
If (Condition) {
Echo statement
} ;
Check all the comparison operators’ examples below: –
<?php
$a=30;
$b=25;
if ($a>$b) {
// code... echo "A is greater";
};
echo "print nothing.";
?>
Explanation: – In the above code, if variable ‘a’ will be greater than variable ‘b’. then will be ‘A is greater’ otherwise, ‘print nothing’.
So, we can use different comparison operators and try them. for example,
==
<?php
$a=30;
$b=25;
if ($a==$b) {
// code... echo "A is equal to b";
};
echo "a is not equal to b.";
?>
The above code will check if both numbers are equal or not. So, we put == to check their equality.
===
<?php
$a=30;
$b="25";
if ($a===$b) {
// code... echo "A is equal to b";
};
echo "a is not equal to b.";
?>
The above code will check if both values and also their data types are the same or not. The output of the above program will be ‘a is not equal to ‘b’. because ‘a’ is an integer whereas ‘b’ is a string
!=
<?php
$a=30;
$b=25;
if ($a!=$b) {
// code... echo "A is not equal to b";
};
echo "condition did not match";
?>
The above code will print ‘a is not equal to ‘b’ because both values are not equal.
<=
<?php
$a=30;
$b=25;
if ($a<=$b) {
// code... echo "A is greater to b";
};
echo "no match";
?>
The above code will print ‘a is greater to b’ because a is greater than or equals to b.
Logical operators
In logical operators, we have to give two conditions. They are used with IF conditions. So, In PHP we have to give two conditions using IF statement So, logical operators check both these conditions and give the result in True or False.
Examples are given below.
Operator | Description | Example |
and/&& | Called Logical AND operator. If both the conditions are true then the result will be true. | (A and B) is true. |
or/ || | Called Logical OR Operator. If any of the two conditions are true then the result will be true. | (A or B) is true. |
! | Called Logical NOT Operator. Use to reverse the logical state of its operand. If a condition is true then the Logical NOT operator will make false. If a condition is false then the Logical NOT operator will make true. | !(A && B) is false. |
xor | Called XOR (exclusive or). If both conditions are whether true or false, the result will print false. If any of the two conditions are true, the result will print true. |
Example of each logical operator.
<?php
$salary=30;
$experience=25;
if ($salary>$experience && $experience<$salary) {
// code... echo "True";
};
echo "False";
?>
The result will be ‘true’ because both conditions are true. You can also use ‘and’ in place of ‘&&’.
<?php
$salary=30;
$experience=30;
if ($salary>$experience || $ salary ==$ experience) {
// code... echo "True";
};
echo "False";
?>
The result will be ‘true’ because one of the above conditions is true. You can also use ‘or’ in place of ‘||’
<?php
$salary=30;
$experience=30;
if (!($salary>$experience)) {
// code... echo "true";
};
echo "false";
?>
The result will print ‘false’ because the condition is false. So, the program will not print if statement.
<?php
$salary=30;
$experience=25;
if ($salary>$experience xor $experience<$salary) {
// code... echo "True";
};
echo "False";
?>
The result will print ‘false’ because both the conditions are ‘true’
Assignment operators
The assignment operators are used to assign value to a variable.
Operator | Name | Example | Explanation |
= | Assign | $a = $b | The value of the right operand is assigned to the left operand. |
+= | Add then Assign | $a += $b | Addition same as $a = $a + $b |
-= | Subtract then Assign | $a -= $b | Subtraction same as $a = $a – $b |
*= | Multiply then Assign | $a *= $b | Multiplication same as $a = $a * $b |
/= | Divide then Assign (quotient) | $a /= $b | Find quotient same as $a = $a / $b |
%= | Divide then Assign (remainder) | $a %= $b | Find remainder same as $a = $a % $b |
= assignment operator
<?php
$a=25;
echo $a;
?>
+= add and assignment operators
<?php
$a=25;
$a+=10;
echo $a;
?>
-= subtract and assignment operators
<?php
$a=25;
$a-=10;
echo $a;
?>
*= multiply and assignment operators
<?php
$a=25;
$a*=10;
echo $a;
?>
/= divide and assignment operators
<?php
$a=25;
$a/=10;
echo $a;
?>
Bitwise operators
The bitwise operators are used to perform operations on bits. Bitwise operator converts the given decimal numbers to their binary numbers and then they perform the operations on those bits and return the result in form of decimal.
Operator | Name | Example | Explanation |
& | And | $a & $b | Bits that are 1 in both $a and $b are set to 1, otherwise 0. |
| | Or (Inclusive or) | $a | $b | Bits that are 1 in either $a or $b are set to 1 |
^ | Xor (Exclusive or) | $a ^ $b | Bits that are 1 in either $a or $b are set to 0. |
~ | Not | ~$a | Bits that are 1 set to 0 and bits that are 0 are set to 1 |
<< | Shift left | $a << $b | Left shift the bits of operand $a $b steps |
>> | Shift right | $a >> $b | Right shift the bits of $a operand by $b number of places |
How bits are calculated?
They are calculated in the bits of 2. Suppose we want to calculate bits of 2 and 4. Now we will put 1 underneath those numbers who make sum of 2. And 0 underneath rest of the numbers like done below.

Now we got bits of 2 is 10. And bits of 4 is 100. Hope you are now clear with how to calculate bits of any number. we will calculate all bitwise operators based on this analysis.
&: Bitwise & operator
If both LHS and RHS operands are 1 then the result will be 1, otherwise the result will be 0.

<?php
echo 1 & 1; // result will be 1
echo 1 & 0;// result will be 0
?>
|: Bitwise or operator
If both LHS and RHS operands are 0 then the result will be 0, otherwise the result will be 1.

<?php
echo 0 | 1; // result will be 1
echo 0 | 0;// result will be 0
?>
^: Bitwise exclusive or operator
If both LHS and RHS operands are same then the result will be 0, otherwise the result will be 1.

<?php
echo 0 ^ 1; // result will be 1
echo 0 ^ 0;// result will be 0
?>
~: Bitwise complement operator
Add 1 to the given number and change the sign.
<?php
echo ~4; //result will be -5
?>
Bitwise complement operator works on single operand
<<: Bitwise left shift operator
Shifts the bits of first number to the left by the number of positions indicated by second number. If we want to calculate bitwise left shift operator for 4 shifted by 2 positions.

So, the output of 4 shifted by 2 positions will print 16. Now, let us see how it will be done in a code.
<?php
echo 4<<2;
?>
“>>”Bitwise right shift operator
Shifts the bits of first number to the left by the number of positions indicated by second number.
No, let us calculate bits of 16 shifted to right by 4 position.

So, the output of 16 shifted to the right by 4 positions will print 1. Now, let us see how it will be done in a code.
<?php
echo 16>>1;
?>
String operators
String operator are used to connect different strings or any data type.
Operator | Name | Example | Explanation |
. | Concatenation | $a . $b | Concatenate both $a and $b |
.= | Concatenation and Assignment | $a .= $b | First concatenate $a and $b, then assign the concatenated string to $a, e.g. $a = $a . $b |
Concatenation
<?php
$a="My name";
$b=$a." is";
$c=$b." John";
echo $c;
?>
Concatenation and Assignment
<?php
$a="My name";
$a.=" is";
$a.=" John";
echo $a;
?>