PHP operators and expressions – Learning PHP for absolute beginners

Gears - image by Alvaro Heinzen

This time around we’ll tackle one more rather interesting and really crucial notion in PHP and for that matter in any other programming language. PHP operators allow you to perform different operations with numeric variables. It is necessary to specify two operands and the appropriate symbol of mathematical operation. For example, the operation of addition (+) can be performed as follows: $x + $y. An operator is a symbol that manipulates 2 or more values.We can group PHP operators into ten types: arithmatic operators, assignment operators, comparison operations, incrementing/decrementing operators, and logical operators. Do you feel like making new friends today? 🙂 Let’s meet the first group.

Arithmetic operators

You are actually familiar with all the operators of this group from your school maths lessons

Operator Name Example Result
+ Addition $x + $y Sum of $x and $y
Subtraction $x – $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y

Assignment operators

Don’t confuse it with the “equal to” operator in PHP, which is written like this ==. The assignment operator means that the left operand gets set to the value of the expression on the right, like this example

$test = 5 + 6;

The variable $test is assigned the result of addition operation, so if we echo() the variable – the result would be 11.

PHP also allows us to combine the assignment operator (=) with some other operators to get a combined assignment operator. Let’s see some examples:

$x = 2;
$x += 5; // you could write it like this $x = $x + 5;
echo $x; // the result in the browser is 7

$a = 'Hello, ';
$b = 'world!';
$a .= $b ; // you could write it like this $a = $a . b;
echo $a; // the result in the browser "Hello, world!"

Comparison operators

It’s clear from the name that these operators allow us to compare one operand with another in multiple ways

Operator Name Example Result
== Equal $x == $y TRUE if $x is equal to $y
=== Identical $x === $y TRUE if $x is equal to $y, and they are of the same type
!= or <> Not equal $x != $y, $x <> $y TRUE if $x is not equal to $y
!== Not identical $x !== $y TRUE if $x is not equal to $y, or they are not of the same type
< Less than $ x < $y TRUE if $x is strictly less than $y
> Greater than $ x > $y TRUE if $x is strictly greater than $y
<= Less than or equal to $ x <= $y TRUE if $x is less than or equal to $y
>= Greater than or equal to $ x >= $y TRUE if $x is greater than or equal to $y
$x = 23;
echo ( $x < 24 ) . '
'; // Displays 1 (true) echo ( $x < "24" ) . '
'; // Displays 1 (true) because PHP converts the string to an integer echo ( $x == 23 ) . '
'; // Displays 1 (true) echo ( $x === 23 ) . '
'; // Displays 1 (true) echo ( $x === "23") . '
'; // Displays "" (false) because $x and "23" are not the same data type echo ( $x > = 23 ) . '
'; // Displays 1 (true) echo ( $x > = 24 ) . '
'; // Displays "" (false)

Incrementing/Decrementing operators

This type of operator allows to add or subtract the value 1 (one) over and over. It’s frequently used when creating loops. The incrementing operator is written as two plus signs, the decrementing one – two minus signs

It’s worth mentioning that the position of the operator makes a lot of difference. If the operator goes before the variable name, then the variable’s value is incremented/decremented before the value is returned.

$x = 5;
echo ++$x;  // Displays “6” (and $x now contains 6)

If the operator is placed after the variable, then it returns the the current value of the variable and then add/subtracts one from the this value

$x = 5;
echo $x++;  // Displays “5” (and $x now contains 6)

Logical operators

These operations are designed to work solely with logical expressions and return either “false” or “true”.

1 == 1 // true
3 > 2 // true
"hello" == "goodbye" // false

Also, the following values are considered to be “false”:

  • The literal value false
  • The integer zero (0)
  • The float zero (0.0)
  • An empty string (” “)
  • The string zero (“0”)
  • An array with zero elements
  • The special type null (including any unset variables)

PHP has 4 logical operators:

Operator Example Result
&& (AND) $x && $y; $x AND $y true if both $x and $y evaluate to true; false otherwise
|| (OR) $x || $y; $x OR $y true if either $x and $y evaluate to true; false otherwise
xor $x xor $y; true if $x or $y (but not both) evaluate to true; false otherwise
! !$x true if $x is false; false if $x is true

And some examples of using PHP logical operators

$x = 2;
$y = 3;
echo (($x > 1) && ($x < 5)) .  "
"; // Displays 1 (true) echo (($x == 2) or ($y == 0)) . "
"; // Displays 1 (true) echo (($x == 2) xor ($y == 3)) . "
"; // Displays "" (false) because both expressions are true echo (!($x == 5 )) . "
"; // Displays 1 (true) because $x does not equal 5

Well, that was a mouthful! Do you guys have anything to say? The comments section is waiting for you. 🙂

About The Author

Tobias

I'm keen on developing websites. Any sentence that has HTML/CSS/PHP in it, will most likely generate interest for me )