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

|
|
 |
php, shoutbox problems Author: vanhansen Posted: Nov 17th, 1:30am Activity: 5 replies, 106 views
|  | MarkupGeeks Logo Author: ahstanford Posted: Nov 16th, 8:45pm Activity: 13 replies, 197 views
|  | Drawing Tutorials Author: ahstanford Posted: Nov 16th, 12:46am Activity: 0 replies, 119 views
|  | Superbowl predictions, anyone? Author: ahstanford Posted: Nov 15th, 10:46pm Activity: 10 replies, 171 views
|  | Photomanipulation Footsteps Author: ahstanford Posted: Nov 15th, 10:43pm Activity: 4 replies, 122 views
|  | Learning to draw... Author: ahstanford Posted: Nov 15th, 12:43pm Activity: 4 replies, 138 views
|  | Looking for simple UI elements Author: FenixRoA Posted: Nov 15th, 6:40am Activity: 7 replies, 128 views
|  | HDD Help? Author: Phoenix Wynde Posted: Nov 13th, 2:31am Activity: 1 replies, 126 views
|  | Fun New Battles Posted! Author: ahstanford Posted: Nov 11th, 7:33pm Activity: 0 replies, 156 views
|  | 4-man Simon Tournament Author: ahstanford Posted: Nov 11th, 3:28pm Activity: 0 replies, 103 views
|  | Design Brief Inspiration for BioRUST Battles! Author: ahstanford Posted: Nov 11th, 7:19am Activity: 4 replies, 153 views
|  | The BioRUST Free Stock Photography Thread Author: ahstanford Posted: Nov 11th, 6:32am Activity: 2 replies, 173 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 212 |
| Total Downloads: | 415 |
| Linkbase Links: | |
 |
|
 |
 |
|