Showing posts with label php command. Show all posts
Showing posts with label php command. 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 

PHP Echo Command - PHP Lesson 1

The PHP Echo command
By Stephon Unomon

We’re going to tell PHP to output something to the screen. Keep in mind that PHP can be used in conjunction with HTML, but we are not showing the code in this example, to keep it simple:

 

<?php echo “Creamy Bagels”; ?>

 

(I hate the “Hello World” sample that every other book in the world uses, so I’m using “Creamy Bagels” instead.)

 

       Let’s dissect the command example bit by bit, shall we? It helps to do this when you are looking at a LOT of PHP code...because, trust me, it can look like a jumbled blurry mess sometimes if you don’t take it piece by piece!
 

<?php - tells the server to process this as php code...

 

echo “Creamy Bagels” ; - tells the server to write what’s in the quotes to the screen, and that the semicolon is ending this particular command...

 

?> - tells the webserver, “OK, I’m done with PHP for now. Back to regular HTML”.

 

Pretty simple when you look at it that way, yes?

 

 

“OK, that’s cool..but what if I want to see quotes on my screen?”

 

 

This can be done by “escaping” the PHP code for what you want to show up in quotes. Let’s use the example from above...

 

<?php echo “Creamy Bagels”; ?>

If you wanted to see quotes around Creamy Bagels, you would use the following code instead:

 

<?php echo “\”Creamy Bagels\””; ?>

Using \” tells PHP that you want a quotation mark to appear. Remember this - it is important, and will be used in the later lessons.