PHP Operators

In any language, human and computer, the way we put words together it’s very important. Like the way people use conjunctions in spoken language to properly say what they mean, the same way PHP uses operators to perform an action. An operator is a symbol or series of symbols that, when used along with some values, performs an action and usually produces a new value. The values used with the operator are called operands or in other words you can say operators are used to operate on values.

You know, you already familiar with Operators and already played with it so far, don’t be surprised ! I tell you. You have already used the assignment operator “=” in previous examples to assign values to variables and the string concatenation operator “.”

Now you got it ! Now first have a look at the below chart and then we continue further.

This section lists the different operators used in PHP.

Arithmetic Operators

Operator Description Example Result
+ Addition x=2
x+2
4
- Subtraction x=2
5-x
3
* Multiplication x=4
x*5
20
/ Division 15/5
5/2
3
2.5
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0
++ Increment x=5
x++
x=6
Decrement x=5
x–
x=4

 
Assignment Operators

Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y

 
Comparison Operators

Operator Description Example
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true

 
Logical Operators

Operator Description Example
&& and x=6
y=3(x < 10 && y > 1) returns true
|| or x=6
y=3(x==5 || y==5) returns false
! not x=6
y=3!(x==y) returns true

 
When you say “two plus three”, in PHP you write it this way: “2 + 3” – the numbers “2” and “3” are the operands, and the addition symbol “+” is the operator. The combination of operands with an operator to manufacture a result is called an expression. Therefore, you can say that an expression is any combination of functions, values, and operators that resolve to a value. The numbers “2” and “3” are also expressions; so to speak, if you can use it as if it were a value, then it is an expression.

 
Assignment Operator

One of the most used operators in PHP is the assignment operator “=”. You can use this to initialize a variable; the operator evaluates the right-hand operand and assigns it to its left-hand operand. While most operators use their operands to come up with a result, and don’t change their values in any way, assignment operators break this rule. Because you can include nest expressions, you can perform multiple things at the same time. So not only you can assign the word “John” to the variable “$name”, but you can output the variable at (almost) the same time. For example:

print ($name = “John”); //assigns “John” to
 
“$name”, then outputs “$name

 
Arithmetic operators

Arithmetic operators are also very used in any programming language. You know for sure what each of them can do, so we’ll just stick to pointing them out: addition “+”, subtraction “-”, division “/”, multiplication “*”, and modulus “%”. Appending additional characters to a str ing is performed using the concatenation operator, which is a single dot “.”. Treating both operands as str types, it appends the right-hand operand to the left, the result being, of course, a str ing.

$first_name = “John”;
 
$last_name = “Davis”;
 
print $first_name . $last_name; //outputs “John Davis”

Although there is one assignment operator, there are a lot of combination operators used with the assignment operator and the arithmetic operators. A combined assignment operator consists of a standard operator symbol followed by an equals sign. Combination assignment operators save you the trouble of using two operators yourself. For example:

$x = 5;
 
$x = $x + 5; //$x now equals 10

The second statement can be wrapped up into a much smaller statement:

$x = 5;
 
$x += 5; //$x now equals 10

You can do the same with other arithmetic operators. While “+=” adds the value of the right operand to the left operand, “-=” subtracts it, “*=” multiplies it, “/=” divides it, “%=” uses the modulus function, and finally “.=” is used to concatenate two str types, appending the right operand to the left one. Another two important combined operators are “++” and “–“. They’re mostly used in conjunction with variables, and increment or decrement the value by one. The operand can either be located at the left side (pre-increment or pre-decrement operator) or the right side of the operator (post-increment or post-decrement operator).

$x = 5;
 
$x++; //$x now equals 6
 
++$x; //$x is now 7
 
$x–; //$x is down to 6

 
Comparison operators

Comparison operators perform tests on their operands. They return the boolean value “TRUE” if the test is successful, or “FALSE” otherwise. This type of expression is useful in control structures, such as “if” and “while” statements. Besides the basic “<” and “>”, “<=”, “>=”, there are some other operators you should know about. “==” means equivalence, “!=” means non-equivalence, “===” means left is equivalent to right and is the same type. These operators are mostly used with integers or doubles, although you can use the equivalence operator to compare str ings.

 
Conditional Operator

You can use a conditional operator in conjunction with the increment or decrement operator. Pay attention to the location of the assignment operator. If you’re using pre-increment or pre-decrement (the operand is at the right side of the operator), then PHP increments or decrements the variable, then performs the test. On the other hand, if you use post-increment or post-decrement operators, PHP will perform the test, and after that it will increment or decrement the variable, regardless of the result of the test.

$x = 1;
 
$bool_var = $x++ < 2; //TRUE, because PHP first performs the test, then increments $x
 
$y = 9;
 
$bool_var = –$y > 8; //FALSE, because PHP decrements $y to 8, then performs the test

 
Logical Operators

Logical operators allow the comparison between booleans. The “or” operator “||” returns “TRUE” if either the left or the right operand is true. The “and” operator “&&” returns “TRUE” if both left and right operands are true. They are mostly use to test two or more expressions that resolve to a boolean. The “not” operator “!” returns true if the expression is false, so in a matter of speaking, it reverses the value of the boolean expression. If the operands consist of two or more expressions, you should use parenthesis to separate them:

($x > 5) && (!($X < 20))

The following conditional operator can seem very odd at first, but it is very useful.

$first ? $second : $third

If the value of the first expression is TRUE, then the second expression is evaluated, and that is the result of the conditional expression. Otherwise, the third expression is evaluated, and that is the value.