| Introduction to PHP: Logical Operators |
pages (2): 1 [2] |
|
|
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!
- Tutorial written by Scrowler
| 
There are no comments for this tutorial yet. You can place a comment by clicking here.
|

|
|
 |
TrentonCS Author: ahstanford Posted: Nov 06th, 7:40pm Activity: 3 replies, 27 views
|  | Html 5 Author: ahstanford Posted: Nov 05th, 1:32pm Activity: 5 replies, 89 views
|  | What are your favorite websites? Author: ahstanford Posted: Nov 05th, 12:51am Activity: 0 replies, 47 views
|  | What do you do for a living? Author: ahstanford Posted: Nov 04th, 11:04pm Activity: 0 replies, 45 views
|  | What is your favorite Subway sandwich? Author: ahstanford Posted: Nov 04th, 11:02pm Activity: 4 replies, 88 views
|  | Windows 7 Author: ahstanford Posted: Nov 04th, 10:59pm Activity: 0 replies, 49 views
|  | Google Wave Author: ahstanford Posted: Nov 04th, 10:52pm Activity: 0 replies, 60 views
|  | University Project Author: Gjbphp Posted: Nov 03rd, 8:59pm Activity: 1 replies, 90 views
|  | Hello BioRust! Author: ahstanford Posted: Nov 02nd, 5:39pm Activity: 4 replies, 87 views
|  | Illustrator cs4 - Convert outlines/graphics to ... Author: izidrew Posted: Oct 29th, 3:48pm Activity: 3 replies, 259 views
|  | Hello BioRust!! Author: MOST Posted: Oct 29th, 12:52am Activity: 4 replies, 144 views
|  | Hey! newbie Author: prelimiting Posted: Oct 28th, 11:51pm Activity: 1 replies, 113 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 212 |
| Total Downloads: | 415 |
| Linkbase Links: | |
 |
|
 |
 |
|