| Downloaded from www.biorust.com on Sun Nov 22, 2009 18:41:16 |
![]() | |
| Introduction to PHP: Logical Operators Tutorial Author - Scrowler (http://forums.biorust.com/member.php?userid=66) |
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.
Let’s say you wanted to do this script in reverse order, i.e. output 50
to 1 inclusive. We simply define $i as 50, change the matching
operator to greater than or equal to 1, and use $i-- instead of
$i++.
|
<?php for ($i = 50; $i >= 1; $i--) echo $i . "<br />"; ?> |
The last loop structure I will look at is FOREACH.
This structure is only used for arrays, to iterate over each value in
the array. The logic for this structure is: FOREACH [array] is
[variable]. So, let’s use it to build an array first, then output
each value, and produce what we did in the previous examples:
|
<?php $numbers = array(); for($i = 1; $i <= 50; $i++) $numbers[$i] = $i; foreach($numbers as $currentnum) echo $currentnum . "<br />"; ?> |
Done! That wasn’t too hard was it? $numbers is
the name of the array, and $currentnum contains the current array
data. This script does exactly the same as the two examples for WHILE
and FOR, except it uses an array to hold the numbers. This example
wouldn’t be as practical as the others as it would probably be slower,
but FOREACH is good for array handling.
The last topic I will cover in this tutorial is SWITCH structure.
Switches
Switches work basically the same as a whole bunch of IF/ELSEIF/ELSE
statements. Using SWITCH(), you define a case for each possibility, and
if you need/want to, a default case that handles all other values. Going
back to our first example with the money jars, let’s write a switch
using those guidelines, including an error handling “default” case.
Default cases must go last.
|
<?php switch($money){ case "note": $notes .= "<br />" . $money; break; case "coin": $coins .= "<br />" . $money; break; default: echo "What kind of money is this? It’s not note or coin form!"; break; } ?> |
This code says what the others say, but if $money
isn’t a coin or note, is outputs an error message! Cool, huh?
I don’t like it when people use many ELSEIF’s to represent complex
choices, so I encourage people to use SWITCH instead. This is why I
haven’t included any ELSEIF explanations in my tutorial.
Anyway, best of luck with PHP! If you are interested and want to learn
more advanced features of PHP, there are plenty of tutorials here at
BioRUST that will help you in most areas of PHP, many that I have
written personally. There are a couple of advanced tutorials covering
GDlib, Cryptography and mcryptlib too, so don't be shy!
You can contact me for help via the
Creative Forums, or click my name below to visit my profile, where
you can email or PM me. Have fun!