Tuesday 26 February 2013

PHP Tutorials (PART 2)


We have already started PHP tutorials. 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:
<?php
$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:
<?php
echo 14%10;
?>
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).
They are:
  • $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:
<?php
echo 5>4;
echo "\n";
echo 5==4;
echo "\n";
echo 5==="5";
echo "\n";
echo 4>=4;
echo "\n";
?>
Outputs this:
1
1
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:
<?php
$a = 12323;
$b = 0;
echo $a&&$b;
echo "\n";
echo $a||$b;
echo "\n";
echo !$a;
echo "\n";
?>
Outputs:
1
That is, false, true and false. Again, if you don’t see why go over each step until you understand it.
We will see more about programming with PHP in the tutorials to come.

No comments:

Post a Comment