Showing posts with label advanced php. Show all posts
Showing posts with label advanced php. Show all posts

Saturday, December 8, 2012

PHP Loops - Advanced PHP For Beginners Lesson 8

LOOPS
By Stephon Unomon

 

       We all have mundane, repetitive tasks we have to do. You know, like putting stamps on all those holiday greeting cards we send out every year? Well, from a programming angle, PHP can help us ease the workload on repetitive tasks in websites with a LOOP.

 

The first one we’ll discuss is the WHILE LOOP. It sounds like a weird carnival ride (I got sick on the While Loop - it was great!) but it is one of the most useful loop functions in PHP.

 

Logically, it looks like this:

 

while ( this conditional statement is true){

          //do this;

}

 

This isn’t real code - just an illustration of how it works. now, here’s what happens, step by step:

 

1. PHP checks the conditional statement. If it is true, move to step 2. If it is false, go to step 4.

2. PHP runs the code contained in the loop.

3. We go back to step 1 and start again, making a LOOP.

4. Once the conditional statement becomes false, the loop exits, and code placed after the loop runs.

 

Real World Usage for WHILE LOOPS

 

       The Creamy Bagel Company wants to show a pricing matrix on their website for up to a 20 bagel pack. But, since bagels are a valuable commodity, the price fluctuates regularly. You have to change it. You love your job.

 

Using a While Loop and some HTML, this can happen with relative ease, and you’ll only have to change one value when the bagel price changes (then charge for 3 hours of work!).

Read on...

 

<?php

$bagel_price = 2.15; //This is the price of one bagel

$counter = 1;

 

echo "<table border=\"1\" align=\"center\">"; //note the escapes for quotes!

echo "<tr><th>Quantity</th>";

echo "<th>Price</th></tr>";

while ( $counter <= 20 ) { //we just set the counter limit at 20

            echo "<tr><td>";

            echo $counter;

            echo "</td><td>";

            echo $bagel_price * $counter; //here we multiply

            echo "</td></tr>";

            $counter = $counter + 1; //here we add 1 to the counter and start again

}

echo "</table>";

?>

 

Our actual Loop code is highlighted in blue above. You’ll see that we set the variables $bagel_price and $counter. The $counter variable allows us to create a math function to increment the unit price by 1 each time we loop (see the $counter = $counter + 1 part at the bottom of the loop?)

So, while $counter equals less-than-or-equal-to 20, this loop will function. Once we hit 21, that’s it - no more loop!

 


Note on PHP Math functions
+  addition (You can use ++ to increment a value by 1)
-   subtraction
*   multiplication
/   division
 
 



 

Here’s what it looks like in a browser...and you can see at-a-glance what 13 bagels will cost you!

 

The FOR LOOP

 

       The FOR LOOP is very similar to a WHILE LOOP. The difference being a little bit more code is contained in the loop. The FOR LOOP can be a little more compact than a WHILE LOOP. Here’s the logic:

 

for ( create a counter; conditional statement; increment the counter){

            do this;

}

 

Let’s use our fluctuating bagel cost from above and write this out. Oops - bagels just went up 10 cents!

 

<?php

$bagel_price = 2.25;

 

echo "<table border=\"1\" align=\"center\">";

echo "<tr><th>Qu antity</th>";

echo "<th>Price</th></tr>";

for ( $counter = 1; $counter <= 20; $counter += 1) {

            echo "<tr><td>";

            echo $counter;

            echo "</td><td>";

            echo $bagel_price * $counter;

            echo "</td></tr>";

}

echo "</table>";

?>

 

Once again, the loop is highlighted in blue. You’ll notice that the counter is defined inside the loop as opposed to a variable outside.

 

And..to the right, you’ll see the output.

 

There’s one more Loop we’re going to cover, isn’t this exciting? Hello? Still there?

 

 

 

The FOR EACH Loop

 

       What if you want to loop through an Associative Array? (See? Told you we’d get back to this!) You can use FOR EACH to do this task. Where the WHILE and FOR loops run until an error is encountered, the FOR EACH loop will run through every element in the array.

 

Let’s revisit the associative array we set up with the trucks...

 

<?php

$truck[“Toyota”] = Tundra;

$truck[“Nissan”] = Titan;

$truck[“Dodge”] = Ram;

?>

 

To loop through the Makers / Models, we’d use this code:

 

<?php

forea ch( $truck as $make => $model){

            echo "Make: $make, Model: $model <br />";

}

?>

The code is written in a strange way, it isn’t obvious how it works. Let’s look at it in a simpler way:

 

$something as $key => $value

 

So, in english...



FOR each thing in the array, I want to refer to the key as $key and the value as $value. The operator ‘=>’ indicates the relationship between the key and the value...the key “points” to the value.

 

I think that’s about all we need to cover with Loops.


The Best GUI Controls 

Introduction To Advanced PHP - Read Before Lesson 6

You Made It Past The Basics - Congratulations!
Please read over lessons 1-5 before starting this section.
By Stephon Unomon

 

       By now, you’ve got enough PHP knowledge under your belt to add basic PHP functionality in all of your websites. It’s pretty cool knowing things that a lot of others don’t know, and they’ll never be able to find out just by doing a “View Source” on your website (especially since you cant actually SEE PHP code by viewing the source in a browser!)

 

       In this section we are going to cover some more advanced PHP code. Things that you might not use just yet but once you are comfortable with PHP and want to get more out of it, you’ll be ready, my young apprentice.

 

       Quick tangent...before we get started here you are going to want to be able to place comments in your code. Why? It’s a heck of a lot easier to know why you wrote a specific line of code if you add comments...so you don’t come back in 3 months and ask yourself, “Was I drunk when I wrote this code??” Here are some examples of comment codes:

 

<?php

// This is a single comment line

# This is also a single comment line

/* This is a block comment, useful if you are working with a multi-line comment or are writing a story on your page that you don’t want people to see */

?>

You don’t have to use semicolons after each line, since the PHP server ignores comments because they aren’t actually commands. Now, on to more CODE! (ta-daaaa!)