PHP Tutorial for Beginners – Part 2
If you missed it, here’s the link to Part 1. So far we covered the basics of PHP, how to run your programs, strings and variables. Today I’ll talk about operators.
An operator, as you certainly know from when you studied maths, is a symbol representing a certain operation. Programming languages, however, how many different types of operators, not only the arithmetic ones, and below I’ll talk about the basic ones.
1. Arithmetic Operators
PHP (and most other programming languages) has all the basic arithmetic operators you are familiar with. Addition is represented by +, subtraction by -, division by / and multiplication by * . So the following piece of code:
$x = 1 + 1;
$y = $x * 4;
$y = $y / $x;
$y = $y - 10;
echo "$x and $y\n";
would print “2 and -6”. If that is not 100% clear to you make sure to go over each stop slowly until you get what’s going on. Notice that when the same variable appears both on the left and on the right side of an attribution, first the right side is computed (using the value currently stored in the variable), and only after that the attribution is performed, changing the value of the variable.
As you probably also noticed the = sign is for attribution. If you want to evaluate the equality of two variables you need to use == (more on that under the “Relational Operators” section).
One very useful operator you might not be familiar with is the modulus one, represented by the % symbol. Basically x % y will return the remainder when x is divided by y. For instance:
Prints 4, because 14 divided by 10 is 1 and has a remainder of 4.
Why is this operator useful? Because it allows you to understand the properties of some numbers and to adapt them to your requirements. For instance, suppose you want to know if any given number is even or odd. You simply need to make that number modulus 2. If the result is 0 the number is even, if the result is 1 it’s odd.
Second, suppose you are working with hours. You have a variable that you keep adding to, but you don’t want it to go past 24. You just need to keep adding and every time you do so you also take the modules 24 of the result, so once it reaches 24 it will go back to 0.
2. Relational Operators
Sometimes these are also called comparison operators, as they compare two different entities (i.e., numbers, variables, strings, etc).
- $a > $b (true if $a is greater than $b)
- $a >= $b (true if $a is greater than or equal to $b)
- $a < $b (true if $a is less than $b)
- $a <= $b (true if $a is less than or equal to $b)
- $a == $b (true if $a is equal to $b)
- $a != $b (true if $a is not equal to $b)
- $a === $b (true if $a is equal to $b and has the same type)
- $a !== $b (true is $a is not equal to $b, or if they are not of the same type)
So the following piece of code:
That’s a true, false, false, true. The boolean true is converted to the string “1”, while the boolean false is converted to an empty string “”, that’s why only 1s were printed. False is equivalent to 0 as well, though.
3. Logical Operators
The three basic logical operators are:
- $a && $b (called the AND operator, it returns true if both $a and $b are true (i.e., true here means not zero)
- $a || $b (called the OR operator, it returns true if either $a or $b are true
- !$a (called the NOT or negation operator, it returns true if $a is false (i.e., 0)
So the following piece of code:
$a = 12323;
$b = 0;
That is, false, true and false. Again, if you don’t see why go over each step until you understand it.
There are the three basic types of operators, and with them you can already create most programs. If you want to read about the other types of operators visit this section of the PHP manual.
Browse all articles on the Software category or check the recommended articles for you below:
6 Responses to “PHP Tutorial for Beginners – Part 2”
Nice tutorial !
awesome tips. thanks for shearing.
Leif G.S. Notae
Ugh, I keep forgetting to tackle this. Thanks for the reminder, looks as though I’ll have more work in my work heh
Thanks for sharing, great article.
I am enjoying your posts, especially the PHP tutorials. Great way to learn.
Thanks for sharing 🙂
Its awesome that you’re sharing this information about PHP, and getting it in small chunks like this is the best way for people to learn.
It’s pretty difficult to do any kind of major customization of your WordPress site or blog without a solid understanding of PHP. I plan to follow along as a refresher!
great…now i am confident that i can learn php.
I tried one year ago but found it was too difficult for me.
Comments are closed.