PHP Lessons Part 2: Operators


Welcome to Part 2 of the PHP lessons.

In the previous lesson, we talked about how to get started with PHP, setting up a local environment to develop in PHP. We also talked about Variables and how they can be very useful when developing.

Just to refresh yourself up, the way we write a variable is like so:

$myVariable = "This is some fancy text";
echo $myVariable;

Now, you should take a look at something, and that something is the Assignment Sign ‘=’. In php, this is called an operator. There are many operators in PHP that will help you determine is something is equal, less than, greater than, assign values, etc. I will list most of the operators I know in this lesson.

Let’s have a look at the variable above, in this case, the equal sign doesn’t mean the Variable “is equal” to the text, what it really means is,

“has the value (that follows)” therefore you could read it as: “$myVariable (has the value) This is some fancy text

So basically, the variable is the same as the text, so we output it and we get whatever is stored in it. I know it still doesn’t make sense for some of you but trust me, it will soon enough.

Below is a list of operators that exists in PHP and their respected meanings.

= Is the same as (or, has the value)

== Is equal to (this is what = traditionally represents, but not in PHP)

=== Is equal to (this one is a strict "is equal to". More on this one below)

!   Not

||  Or

&&  And

>   Is greater than

<   Is less than

!=  Is not equal to (in content)

!== Is not equal to (in content and type)

>=  Is great or equal to

<=  Is less than or equal to

+   Addition

-   Subtraction

*   Multiply

/   Divide

%   Modulus (I don't really use it, but it's useful e.g. finding if a number is even)

++  Increment

--  Decrement

In those list, there are four types of operators, that is Arithmetic Operators, Assignment Operators, Comparison Operators and Logical Operators.

== , != , > , < , >=, <= , those operators are Comparison Operators and return true or false. In programming language, we would say it returns a Boolean Value (true or false are boolean values).

Some example of Comparison Operators Below:

2 == 4 This will return false since 2 is not equal to 4.

2 != 4 This will return true as 2 is not equal to 4. Remember, the exclamation sign means "not" in PHP

2 > 4  This will returns false obviously. 2 is not greater than 4.

2 < 4  This will return true as 2 is less than 4

2 >= 4 Returns false

2 <= 4 Returns True (You might be wondering how 2 is equal to 4, it returns true only because 2 is less than 4)

Basically, these would be used in Conditions (which come in a later tutorial). 2 > 4 would be asking “2 is greater than 4?”, and the answer to that is (human) “no!” or (PHP) “false!”.

Some example of Arithmetic Operators Below:

(Note, these NOT valid PHP syntax as such, but we are just showing basic operations here)

a+2 The new value of 'a' will be 7 since variable 'a' contains the value of 5, thus 5 + 2 = 7

a-2 The new value of 'a' will be three, 5 - 2 = 3

a*4 The new value of 'a' will be 20. 5 * 4 = 20

10/2 The answer will be 5
5%2 The answer will be 1
10%8 The answer will be 2
10%2 The answer will be 0

Note that % represents the remainder after division, and it is this remainder that is returned. E.g. 5%2 is saying “5 divided by 2 gives 2 and remainder 1, so return that remainder”

a++ The new value of 'a' is 11, since ++ will increment our value by 1 every time it is used.

a-- The new value of 'a' will be 9, it will subtract 1 everytime it is used.

Some example of Logical Operators Below:

a=10, b=3
(a < 11 && b > 2)

This will return true because 10 is less than 11, and 3 is greater than 2. For the && operator (AND operator), both must be TRUE for the whole result to be TRUE. An analogy would be “coffee is drink AND water is drink AND bread is drink?” which would be FALSE since the third part is false. There can be more than 2 comparisons!

Oh, you can mix and match those operators depending on what you want to get. For example, this would be perfectly valid:

( (a == 3 && b == 4) || (a == 4 && b == 5) )

which would return true in the cases where a is 3 and b is 4, and a second case, where a is 4 and b is 5.

If you look closely, you will notice that I used multiple brackets. Brackets are used here to indicate operator precedence. More on this will come in a later tutorial, but basically, it means

a=10, b=3
(a == 5 || b == 2)

This will return false because 10 is not equal to 5 or 3 is not equal to 2. For the || (OR operator), at least one of the comparisons must be TRUE. Again, an example would be “coffee is drink OR water is drink OR bread is drink”, and this would be TRUE, since the 1st and 2nd comparisons are true.

a=10, b=5
!(a == b)

Returns true, what we did here is simple, ‘a’ is not equal to ‘b’ using the ! sign. In fact, we actually said “a is equal to b”, which returns FALSE of course. But then, we took the negation (or inverse if you wish) of that FALSE using the ! sign, which finally returns TRUE.

This is a little introduction to Operators and in the next lessons, we will have a look at IF and ELSE statement and we will be using operators to perform some task.

This post has been contributed by Tipa of Mu-Anime

[seriesposts title=”PHP Lessons” titletag=h3 listtype=ul orderby=date]