Jump to content
xisto Community
Sign in to follow this  

For ... Next Loops And Script Planning My Fifth PHP Tutorial

Recommended Posts

Be sure to read the other ones. They are located in the TUTORIALS section, and at the time of this writing all on the first page.

Intro To PHP
Tutorial 5 - For ... Next Loops and Planning Scripts
Released 4/15/07
By Chris Feilbach aka GhostRider

Contact Info:
E-mail: assembler7@gmail.com
AIM: emptybinder78
Yahoo: drunkonmarshmellows
Website: http://forums.xisto.com/no_longer_exists/

Before I start talking about what wonderful things loops are and all the cool stuff they can do, I want to address something first. I was asked about something to write in PHP. There isn't much you can really write yet, if your learning from my tutorial. You could always process some form data and practice writing conditional statements.

I have a couple main goals in releasing this tutorial. The first being to teach PHP, but I want to teach you more than that. I want you to be able to know what it is really like to program. Programming is a lot of the time taking smaller ideas, like conditional statements and loops and stuff, and putting them together to form a big picture. The things you are learning now will help you best to do that. I believe what makes programmers truly great is their ability to solve problems, and to be able to put together small bits of code that makes something truly useful out of it.

My goal is that after a couple more tutorials you'll know enough to hopefully start building your own scripts. So I'm going to talk about a little bit about how to plan for a script.
The first think I ask myself, or ask a PHP customer of mine, is "What do I/you need my script to do?". Don't over work yourself here. Always start out small until your ready to handle more.

Let's say, for whatever reason, you need a script that displays 5 random numbers between 0 and 60, and tells us whether the number is even or odd.

Think about it for right now. You need not think about it in terms of PHP, or even how a computer program would do it. Think about how you would do it. Take some time, and have an answer in your head before you move on.

My answer in my head was that I would determine what the 5 numbers are, and then look at the numbers. If they end in 0, 2, 4, 6, or 8, then they are even.

Now that you have a good idea in your head on how to do it, lets think of it from a programming aspect.

Think about it again. Don't just look down at what I am going to type.

Firstly, you will need 5 variables for your random numbers. Lets make a numberical array for that to make life easier. We also need to determine whether the number is even or odd. Divide it by two, and if it ends in .5 then it is odd. Otherwise it is even. We can use an if ... then statement for that.

Typing out code five times long is not only a waste of space, its boring even if you are using copy and paste! I am going to introduce to you the programming concept called for ... next loops. For ... next loops have a starting value, a condition which stops the loop, and also the option to either increase or decrease the starting value. $i is almost ALWAYS used for the starting value. Loops usually start out on zero, like arrays.

Like conditional statements, you will want to indent all of your loops. It makes things very easy to read and debug.

<?PHP	for ($i=0;$i<=4;$i++) {	print("Hello World!<br>");	}PHP?>

The above is a for ... next loop. Start with an indent and a for. $i=0; defines $i as the starting variable, and sets it value to zero. $i<=4 is the ending condition. As long as $i is less than or equal to 4 the loop will continute. $i++; means that $i is increased by one everytime to loop is repeated.

One of the biggest problems I had with learning for ... next loops was that the ending condition is true, and once it becomes FALSE the loop ends. Its backwards from other languages I have learned. However loops are important. Now, to our example. You may view it at http://forums.xisto.com/no_longer_exists/

The increment statemnt ($i++) can also be $i-- (subtract one), or something like $i + 1;, or even $i * 2. This statement is weird, it doesn't require a semicolon.

We need to get five random numbers. The function rand(min, max) will give us a random number between min and max. We can use a for ... next loop to populate (fill up) our array. We can then use another loop to figure out whether they are even or odd, and store that in another array. And we can use yet another loop to print the data.

Before we begin coding, I need to introduce some stuff about division in PHP (and many other languages). There are two types, floating point division, and integer division.

Floating point division, which is signified by a forward slash (/), returns a decimal. Integer division does not. Integer division is signified by a back slash (\).

<?PHP$temp = 5 / 4;print $temp; // Prints 1.2$temp = 5 \ 4;print $temp; // Prints 1PHP?>

When using integer division you can use the percent sign (%) to return the remainder.

<?PHP$problem = 5 \ 4;print "5 over 4 equals $problem.<br>";$remainder = 5 % 4;print "The remaind of 5 over 4 is $remainder.";PHP?>

<?PHP// Use a numerical array to collect our data.  Our array is going to start on zero.	for ($i=0;$i<=4;$i++) {	// $i starts on zero, the loop runs until the loop is greater than four.	// We could also have written is like this, ($i=0,$i<>5;$i++) {	// That would run when $i is not equal to five.  It is identical to the loop above.  The <> symbol is the same as != , it means not equal to.	$numbers[$i] = rand(0, 60);	}// We now have our values.  Let's create another array, $evenodd, that holds whether each number is even or odd.  For simplicity, we will put an "o" if it is odd, and an "e" if it is even, but I often use either 0 or 1, like other programmers.	for ($i=0;$i<>5;$i++) {	// Odd numbers, when divided by 2 have a remainder of 1.	$temp = $numbers[$i] % 2;		if ($temp == 0) {		// The number is even.		$evenodd[$i] = "e";		} else {		$evenodd[$i] = "o";		}	}// Just for fun lets print the numbers out in reverse order than we generated them.// Lets make the page nice, too.print("<html><head><title>5 Random Number Project</title></head><body>");	for ($i=4;$i<>-1;$i--) {	print $i . "<br>";	print $numbers[$i];		if ($evenodd[$i] == "e") {		print("  Even");		} else {		print("  Odd");		}	print "<br>";	}print("</body></html>");PHP?>

And there we have it! An awesome random number generator. I'm not quite sure what I want to teach next, but I promise it will be interesting.

Share this post

Link to post
Share on other sites

It's a nice tutorial. Why did you not put this with your other tutorials though?As a php coder myself, I'd have to say for loops suck. I hate them. Sure they save you time, but they just make me angry. They're for people who are too lazy to use a while loop. A while loop can be used for everything, and a for loop is limited to just counting. Nothing against your tutorial though, just ranting on. I'm glad you posted this for those who didn't exactly know what a for loop was. It would have been nice to also have seen a for-each loop explained, (as much as i hate those too). Anyway that's beside the point. Keep up the good tuts!:unsure:

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.