Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Related Links

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

Pages (2): <Prev 1 [2]
Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments


There are no comments for this tutorial yet.
You can place a comment by clicking here.
Amazing Font Pack!

Featured Tutorialsmore

Sunbursts
Sunbursts
- Adobe Illustrator -
Xpresso Tutorial -...
Xpresso Tutorial -...
- Maxon Cinema 4D -
Razor Stryke Logo
Razor Stryke Logo
- Adobe Photoshop -
Pixel Stretch Swirl
Pixel Stretch Swirl
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 887 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1052 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1054 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1935 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1832 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14441 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2751 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2772 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3439 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2356 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2499 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2798 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673