|
Welcome to PHP. This tutorial is aimed at beginner PHP
developers who want to learn how to use the PHP's in-built logical operators.
Let’s say you have two savings jars, one for notes and one for coins. A friend
gives you some money. If it’s a note, you want put it in the notes jar,
otherwise it must be a coin and goes with the coins. How do you code this in PHP?
Well, we simply use an operator called IF!
Before we get any further, lets discuss the logic of operators. To start off you
call the operator. Next you define the arguments. Then you define the
instructions that will be carried out when the arguments are correct. If you
have more than one instruction to be processed in the event of the argument(s)
being correct, you enclose them in curly brackets. i.e. {
/*instructions*/ }. If...Else Statements
Ok, let’s start with the IF operator. We will use our example above and code it.
The logic is the same - i.e. if the money is a note, add it to the note box:
<?php
if($money == "note") $notes .= "<br />".$money;
?>
|
And it’s done. As you only have one instruction, you don’t
need curly brackets. This script will check whether the variable $money
is equal to the text “note”, and if so, adds a “<br />” and
the value of $money to the $notes variable. <br /> is
XHTML for a line drop, so when you output the variable you can see the
individual entries.
.= is syntax that you can use instead of = [] + []. i.e.
instead of typing $var = $var . $iable you can simply replace it with
$var .= $iable. Other similar syntax formats you can use are: +=,
-=, %=, *= and /=.
But what happens when $money is not equal to “note”?. To
handle this, we use the ELSE operator:
<?php
if($money == "note"){
$notes .= "<br />".$money;
} else {
$coins .= "<br />".$money;
}
?>
|
The human translation of this code is quite simple: "if
$money is equal to “note” then add the value of $money to the $notes
variable, otherwise add it to the $coins variable".
To finish up our section on the IF statement, let’s look at how to define
multiple conditions. We can use either && (and) or || (or) or
simply the word “AND” or “OR”:
<?php
if( ($money == "note") || ($money == "coin") ) $piggybank .= $money;
# these are the same functions! (above and below)
if($money == "note" or $money == "coin") $piggybank .= $money;
?>
|
Of course, if you execute the above code, you will get the
value of money appearing twice, because both statements are the same. That
said, let’s move on to looping. Looping
There are 3 main ways to use loop structures. The basic structure is: while
[argument] is true, process instructions then re-iterate.
The first example I will outline uses WHILE, and is possibly the easiest looping
structure. Let’s make a simple number generator that will output the numbers
from 1 to 50 inclusive on a new line. Pleas note that <= means 'less
than or equal to', >= means 'greater than or equal to', ==
means 'equal to', and != means 'not equal to'.
$i++ is a simple instruction, adding 1 to variable $i. You can
also use $i += 1 or $i = $i + 1, if that is your way - it’s just
preference really!
<?php
$i = 1;
while ($i <= 50)
{
echo $i . "<br />";
$i++;
}
?>
|
Simple enough? Good. Let’s move on to FOR statements. The
FOR statement simply incorporates definition of the identifier, the argument
and the last instruction into one line. The logic is: for [define
identifier], [condition], [what to do with identifier] instructions.
Let’s rewrite the above code with the FOR statement instead:
<?php
for($i = 1; $i <= 50; $i++) echo $i . "<br />";
?>
|
Note that I didn’t use curly brackets simply because
there is only 1 instruction now. You can use them if you like, but it
will not affect the output.
- Tutorial written by Scrowler
|