post

Learn PHP Operators

You may be thinking to yourself, why study PHP operators? After all, I think I can figure out what is going on by looking at code examples and I already have a good idea of what it means when I see a plus symbol, minus sign or equals sign. For the most part, I would agree with you. As you write your code and look at examples, you will get a very good idea how to use operators. But having an in-depth understanding of what the operators are and how they perform will make the coding process go much smoother and will get you on the right track to writing optimized code from the beginning. As you will see, when you review the assignment operators below, there are several shorthand ways to perform simple mathematic operations on your variables that will save you time and effort in the long run.
 
 

Assignment Operators

 
PHP assignment operators are used to set the value of a variable. The most commonly used assignment operator is the equals sign, however, you can also use other variables and mathematic expressions while assigning a value.
 
 

Operator

Description

Example

Result

=

Assigns the variable equal to a value. This can be a string, number, array or the value of a previously established variable.

$z=$x;

5

+=

Assigns the variable with a value of itself plus another value or
variable. This example is the short form of ($x=$x+$y;).

$x+=$y;

11

-=

Assigns the variable with a value of itself minus another value or
variable. This example is the short form of ($y=$y-$x).

$y-=$x;

1

*=

Assigns the variable with a value of itself multiplied by another
value or variable. This example is the short form of ($x=$x*$y).

$x*=$y;

30

/=

Assigns the variable with a value of itself divided by another value or variable. This example is the short form of ($x=$x/$y).

$x/=$y;

0.833333333333

.=

Combines the value of the variable with another value or variable. This example is the same as ($x=$x.$y)

$x.=$y;

56

%=

Assigns the variable with the remainder of a division operation performed by dividing the value by another value or variable. This example is the short form of ($y=$y%$x).

$y%=$x;

1

Note: $x = 5 and $y = 6.

 
 

Arithmetic Operators

 
Arithmetic operators are used to perform mathematic operations within your code. You will use arithmetic operators more than you think. In particular, incrementing and deincrementing will be very useful in creating counters for your loop and do statements.
 
 

Operator

Description

Example

Result

+

addition

$x+$y

11

subtraction

$x-$y

-1

*

multiplication

$x*$y

30

/

division

$y/$x

1.2

%

modulus

$y%$x

1

++

Increment by 1, if ++ is used after the variable, then it is
incremented after that line of code is executed.

echo $x++;

5
(the value of $x becomes 6 after echo)

 

If ++ precedes the variable, then it is incremented before the line of code is executed.

echo ++$x;

6

--

Deincrement by 1, if -- is used after the variable, then it is
deincremented after that line of code is executed.

echo $x--;

5
(the value of $x becomes 4 after echo)

 

If -- precedes the variable, then it is deincremented before the line of code is executed.

echo --$x;

4

Note: $x = 5 and $y = 6.

 
 

Comparison Operator

 
Comparison operators are used to compare values and determine if your expression is true or false. These operators are the backbone of making decisions within your code.
 
 

Operator

Description

Example

Result

==

Equal to

$x==$y

false

!=

Not equal to

$x!=$y

true

<> 

Not equal to

$x<>$y

true

Greater than

$x>$y

false

Less than

$x<$y

true

>=

Greater than or equal to

$x>=$y

false

<=

Less than or equal to

$x<=$y

true

Note: $x = 5 and $y = 6.

 
 

Logical Operators

 
Like comparison operators, logical operators are used to determine if an expression is true or false. Their role is a little different because they are often used to evaluate the result of multiple comparison operations in order to make a true or false decision. You will note that there are multiple versions of and and or operators. The reason for this is that && and || have higher precedence’s than the and and or operators.
 
 

Operator

Description

Example

Result

&&

And, can be used to evaluate two or more values. If all values equal true, then the result is true. If any of the values in the expression are false, then the result will be false.

($x>20 && $y==6)

false

||

Or, can be used to evaluate if any of the values in the expression are true. If any value within the expression is true, then the result will be true.

($x>20 || $y==6)

true

!

Not, is used to evaluate if a set of values are not true. If the
expression is not true, then the result of the expression will be true.

!($x>$y)

true

and

And, can be used to evaluate two or more values. If all values equal true, then the result is true. If any of the values in the expression are false, then the result will be false.

($x>20 and $y==6)

false

xor

This version of an or operator is used to determine if only one of the values in the expression is true. If both are true, the result will be false.

($x>20 xor $y==6)

true

or

Or, can be used to evaluate if any of the values in the expression are true. If any value within the expression is true, then the result will be true.

($x>20 or $y==6)

true

Note: $x = 5 and $y = 6; This table’s rows are ordered by operator precedence.

 
 
Logical operators can be used together in order to evaluate multiple conditions at once. For example, if you wanted to ensure that your line of code would only execute if $x is greater than $y or if $x is equal to 5 and $y is equal to 6 you could accomplish this by combining both the || and the && operators as arguments within an if statement.
 

PHP Combining Logical Operators Example

<?php
$x=5;
$y=6;

if ($x>$y || $x==5 && $y==6)
    echo "This statement is true.";
else
    echo "Epic fail!";
?>

 

Result:

This statement is true.

 
 

String Operators

 
As we discussed in Learn PHP, Strings, the only operator specifically for use with strings is the concatenation operator (.). The concatenation operator combines two strings together. If the two strings happen to be numbers, then the numbers will be combined and you can continue to manipulate it as a number.
 

Concatenation Operator with Numbers

<?php
$x=5;
$y=6;

$x.=$y;

echo "\$x becomes: " . $x . "<br />";
echo "\$x divided by 2: " . $x/2 . "<br />";
?>

 

Result:

$x becomes: 56
$x divided by 2: 28

 
 

Operator Precedence

 
Just like in algebra, some operators will have a higher precedence than others. For example, if you are attempting 5 + 6 * 2, the result will be 17 and not 22 because the multiplication operation holds a higher precedence than addition. You can solve this in the same way that you would in normal arithmetic, (5 + 6) * 2 will give you the desired result of 22. When operators have an equal precedence, they will be evaluated left to right in most cases, however, the logical operator ! and all assignment operators are evaluated right to left.
 
 

Speak Your Mind

*

*