| 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.
|

|
|
 |
Photoblogging & Photo Galleries Author: synthetic Posted: Aug 07th, 8:16pm Activity: 3 replies, 22 views
|  | Member Tutorial: If user is logged in. Author: Trueskool Posted: Aug 07th, 3:51pm Activity: 1 replies, 24 views
|  | Help with highlights and shadows Author: ziggyz Posted: Aug 07th, 3:16pm Activity: 0 replies, 16 views
|  | Anybody use Inkscape? Author: guppyman Posted: Aug 07th, 12:22pm Activity: 0 replies, 17 views
|  | Modelling a product button in Rhino3D Author: guppyman Posted: Aug 07th, 12:19pm Activity: 0 replies, 22 views
|  | how to make a watermark signature using photoshop Author: jubach Posted: Aug 07th, 11:43am Activity: 0 replies, 34 views
|  | warm hello Author: jubach Posted: Aug 07th, 11:35am Activity: 1 replies, 25 views
|  | Another Odd Thing Happening Author: synthetic Posted: Aug 06th, 12:52pm Activity: 2 replies, 69 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 210 |
| Total Downloads: | 406 |
| Linkbase Links: | 255 |
 |
|
 |
 |
|