Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> For ... Next Loops And Script Planning, My Fifth PHP Tutorial
ghostrider
post Apr 16 2007, 01:02 AM
Post #1


Super Member
*********

Group: Members
Posts: 397
Joined: 9-June 06
From: Wisconsin
Member No.: 24,924



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://www.ghostrider.trap17.com

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

CODE
<?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://www.ghostrider.trap17.com/tut/php5

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 (\).

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


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

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


CODE
<?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.
Go to the top of the page
 
+Quote Post
galexcd
post May 13 2007, 08:50 PM
Post #2


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,074
Joined: 25-September 05
From: Los Angeles, California
Member No.: 12,251



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!

tongue.gif
Go to the top of the page
 
+Quote Post
slushpuppy
post May 18 2007, 08:07 AM
Post #3


Newbie [Level 2]
**

Group: Members
Posts: 29
Joined: 7-September 06
Member No.: 29,569



Very nice tutorial! How about combining foreach and while loops(If you haven't done before) into this tutorial?
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Mail Form (php)(3)
  2. Php Quiz Script(20)
  3. Email Script/form With Php(35)
  4. Simple Php Counter Script(2)
  5. E-mail Mailer Script 0.1(4)
  6. Secure The Email Addresses On Your Website!(10)
  7. [php] Simple Newsletter Script(8)
  8. How To Use Trap17 Cgi Formmail(15)
  9. Random Quote Script(6)
  10. Installing Ndstats!(4)
  11. Verifying Email Addresses(9)
  12. Page Generation Time Script(17)
  13. Script To Build A List Of Links(2)
  14. Php Menu Bulding Script And Site Template(0)
  15. Php Script To Make A Link List(6)
  1. Using An Actionscript In Flash For Loops(0)
  2. How To: Ip Configuration Script (win Xp)(0)
  3. Image Rotator Script (another One)(0)
  4. Background Image Swap Script(15)
  5. How To Create A Song In Fruity Loops Using Vanguard(1)
  6. Making A Song In Fruity Loops Part 2(0)
  7. Making A Song In Fruity Loops Part Three(1)
  8. Fruity Loops Tutorials-trance Style Songs(11)
  9. Fruity Loops Tutorials-arpiggeator(0)
  10. [phpbb] Member Last-visit Report Script(0)


 



- Lo-Fi Version Time is now: 13th October 2008 - 06:59 PM