# PHP Operators | How to use Operators

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

#### 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

#### 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.

##### 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.

= assignment operator

``````<?php
\$a=25;
echo \$a;
?>``````

``````<?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.

#### 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.

#### 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;
?>``````

PHP Operators | How to use Operators