Final Code Challenge
Posted on 12. Mar, 2010 by admin in Basics
Looks like our teaching time is coming to a close. You have gone from not knowing what PHP is to well versed. You must pass one final test before you are a true PHP Master!
Posted on 12. Mar, 2010 by admin in Basics
Looks like our teaching time is coming to a close. You have gone from not knowing what PHP is to well versed. You must pass one final test before you are a true PHP Master!
Posted on 12. Mar, 2010 by admin in Basics
Ah, another one of those choose one or the other they both do the same thing. We can use the same HTML code, but change the method to GET.
<html> <head> <title>Flipping Forms Tutorial</title> </head> <body> The following is a form: <br/> <form action="index.php" method="get"> What is your favorite book?: <input type="text" name="book" /><br/> What is your favorite movie?: <input type="text" name="movie" /><br/> What is your favorite website?: <input type="text" name="website" /><br/> <input type="submit" /> </form> </body> </html>
Now the difference between GET and POST is a matter of visibility. When we hit the submit the POST data “magically” goes to the next page. With GET the data is put on the end of URL. When you hit submit you should see something like this on the end of your URL ?book=Harry+Potter&movie=Star+Wars&website=WildPHP+of+Course! .To print out the variables like last time we just use $_GET instead of $_POST:
Printed Results:
<?php
//Print out our GET Data
echo “Favorite Book: “.$_GET["book"];
echo “Favorite Movie: “.$_GET["movie"];
echo “Favorite Movie: “.$_GET["website"];
?>
Sample Output:
Favorite Book: Harry Potter
Favorite Movie: Star Wars
Favorite Website: WildPHP of Course!
Now that the data is viewable in the URL it probably won’t be the best idea to send passwords or sensitive data via GET. GET however can be very useful because we can pass information to PHP without having to fill out a form. If you look above at the URL you are at right now you can see GET in use. Using the info in the URL, PHP knows what post to pull up.
If you want to create a GET variable in a URL just follow this simple format: ?varname=value&varname2=value2. Notice that it always starts with a question mark, and additional values can be added with an ampersand(&).
Posted on 12. Mar, 2010 by admin in Basics
In the last tutorial we brushed up on our HTML skills. Lets take the code from the last tutorial, and make a small edit.
<html> <head> <title>Flipping Forms Tutorial</title> </head> <body> The following is a form: <br/> <form action="index.php" method="post"> What is your favorite book?: <input type="text" name="book" /><br/> What is your favorite movie?: <input type="text" name="movie" /><br/> What is your favorite website?: <input type="text" name="website" /><br/> <input type="submit" /> </form> </body> </html>
If you didn’t notice the slight change I made was method=”post”. This specifies the type of data our form should send. Now on to the PHP. Putting some text in box and hitting submit is not going to do us much good, so let’s capture it and print it out. Put this PHP code under the </form> tag.
Printed Results:<br/> <?php //Print out our Post Data echo "Favorite Book: ".$_POST["book"]."<br/>"; echo "Favorite Movie: ".$_POST["movie"]."<br/>"; echo "Favorite Website: ".$_POST["website"]."<br/>"; ?>
Sample Output:
Favorite Book: Harry Potter
Favorite Movie: Star Wars
Favorite Website: WildPHP of Course!
As you can see from the code we get the post data and print it out once you hit submit. The simple $_POST["var name"] statement allows us to do that.
Posted on 12. Mar, 2010 by admin in Basics
For this series of tutorials I said you needed to know HTML. Since I am a nice guy I will teach you the limited HTML we need to know for our next two tutorials. Let’s start with a basic HTML document and then go from there.
<html> <head> <title>Flipping Forms Tutorial</title> </head> <body> The following is a form: </body> </html>
In previous tutorials we have manually entered our integers and strings. For this we needed access to our PHP file to change the values. This would not be particularly helpful to a typical user on your website. HTML forms allow us to submit values to PHP which we can process. Let’s make a simple form
<html> <head> <title>Flipping Forms Tutorial</title> </head> <body> The following is a form: <form action="index.php"> What is your favorite book?: <input type="text" name="book" /> What is your favorite movie?: <input type="text" name="movie" /> What is your favorite website?: <input type="text" name="website" /> <input type="submit" /> </form> </body> </html>
Let’s go through the HTML one part at a time. If you are already well versed in HTML you should be able to skip this tutorial, and move on to the next one on POST.
Action=”index.php” – This line is the document that you are directed to after you hit the submit button. I am assuming you are still using index.php to run your PHP code .If you are not, you will have to modify it to the one you are using. You can direct it to a completely different one to run your PHP code there as well (more in the next tutorial).
<input type=”text” name=”book” /> – In this line we have the textbox type which is text for textbox. We also have a name which is very important. Follow the same rules as the PHP variable names to keep everything simple. We will use this name to access it in PHP so keep it simple and short.
<input type=”submit” /> – Our submit button. You can also add value=”Your Text”, to change the text from “Submit Query”.
Now that you have a little more knowledge with PHP forms, we can move on to more fun things!
Posted on 12. Mar, 2010 by admin in Basics
Me again! I hope you are having fun with loops and arrays, so let’s test your knowledge.
Q: What is a loop?
Q: What is the syntax when using a For loop?
Q: What is the syntax for a While loop?
Q: What is an infinite loop?
Q: What is an array index?
Project One: Write a program that prints out “I Love WildPHP.com!” 10 times. Use While and For loops.
Project Two: Write a loop that will add all the numbers in the following integer array: 22, 56, 7, 32, and 11.
Posted on 12. Mar, 2010 by admin in Basics
At the start of this series of tutorial we learned how to use variable. As you already know variable store information for use for editing and later use. So what happens when you need use a bunch of variable? Let’s say for example you wanted to store a bunch of ice cream flavors, lets say 8. Using what we learned up to this would take a few lines because we would need to create a new variable to store each new flavor. This is where arrays come in handy.
Arrays can store all types of values from string to integers to many different types we have yet to go over. Lets try out a simple array before we move on to our example:
<?php
//One way to declare an array
$apartment = array("Ground Floor","Floor 1","Floor 2","Floor 3");
//Other way to declare an array
$apartment[0]="Ground Floor";
$apartment[1]="Floor 1";
$apartment[2]="Floor 2";
$apartment[3]="Floor 3";
//Print out a floor name
echo $apartment[2];
?>
Output:
Floor 2
Think of an array as a stack of variables. Instead of creating a bunch of variables, we can simply create an array. The individual values in an array, by it’s index. If your confused you can think of an array as a building with individual values on each floor. If we specify we want to go to index or “floor” 0, remember we always start from zero in PHP, we might find a stored value like “chocolate.” If we where to go to 6th index or “floor” we could find a value like vanilla.
<?php
//Declare our array
$flavors = array("Chocolate ", "Strawberry ", "Butter pecan ", "Neapolitan ",
"Vanilla fudge ripple ", "French vanilla ", "Vanilla ", "Cookies and cream ");
//Edit a flavor
$flavors[0] = "Chocolate chip ";
//Print our flavors
echo "Our Flavors: ";
for($i=0; $i<=7; $i++)
{
echo $flavors[$i];
}
?>
Output:
Our Flavors: Chocolate chip Strawberry Butter pecan Neapolitan Vanilla fudge ripple French vanilla Vanilla Cookies and cream
Wwe can do the same thing more efficiently with arrays. We can easily add other ice cream flavor, and have it print out for us without adding additional lines.
Posted on 12. Mar, 2010 by admin in Basics
In the previous tutorial we covered loops. “While” loops are similar, but are best used in different situations. The concept is similar to what we learned earlier concerning if and switch statements, and how they can basically complete the same function, but each is better for certain tasks.
A while loop has a much simpler format than the for loop.Thinking back to the previous tutorial, the second argument in the for statement was a condition. In a while loop this is the only thing we need. Let’s use the example from the previous tutorial and repeat it in a while loop.
<?php
//First Declare the Counter Variable
$i=1;
//Same Conditional Statement as the For Loop
while($i <= 5)
{
echo "This is a test. ";
//Increment must be in the loop or
//we will have an infinite loop
$i++;
}
?>
The comments above should provide some guidance regarding how the while loops are similar to for loops. We cannot declare the counter variable inside the statement, we must declare it before. Also, we must make sure to make the counter variable change inside the loop or it will just go on forever.
It’s best to use while loops when you want the loop to be dynamic rather than static. For our for loop example we counted down from 10 to 0. We knew that we needed to count down from 10 to 0, so it was quite easy to use a for statement to do this. You would use a while if you really did not know.
For example, if you wanted to count from two random numbers, in this case 6 to 17, your code would look something like this:
<?php
$start = 6;
$end = 17;
echo "We are counting from ".$start." to ".$end.". ";
while($start <= $end)
{
echo $start."... ";
$start++;
}
echo " All Done!";
?>
Output:
We are counting from 6 to 17. 6… 7… 8… 9… 10… 11… 12… 13… 14… 15… 16… 17… All Done!
It is up to you to decide if you want to used Wild While Loops or Flash For Loops.
Posted on 06. Mar, 2010 by admin in Basics
Sometimes, once is just not enough. Thankfully, in PHP we have something called loops to help us. Basically, they allow us to repeat portions of code over and over again as much as we would like. For this tutorial let’s use two examples. Let’s say you want to first print out a simple statement five times. In regular PHP code, not using loops, it would look something like this:
<?php echo "This is a test. "; echo "This is a test. "; echo "This is a test. "; echo "This is a test. "; echo "This is a test. "; ?>
Output:
This is a test. This is a test. This is a test. This is a test. This is a test.
If we needed to print even more of this statement we would need to use even more lines of code. With a simple statement we can print out the same lines as many times as we want.
<?php
for($i = 1; $i <= 5; $i++)
{
echo "This is a test. ";
}
?>
Output:
This is a test. This is a test. This is a test. This is a test. This is a test.
Just as like if statement you put the code you want to run in-between two brackets. For ( counting variable; conditional statement; increment counting variable). In this case $i is our variable. We do a conditional just like in an if statement. If the variable $i is less than or equal to 5, then the code in the brackets will keep running. If not it will move on past the for statement. The last one adds one to the variable every time to code is run in the loop. We can also subtract one like this $i–; If you where to do that in this statement it would cause an infinite loop! This is where the middle condition is never met and the loop keeps repeating. This is because the $i variable would go 0,-1,-2,-3, etc. and always be less than 5.
We can also easily use variables to make for statements do much more for us. See the following example:
<?php
echo "Countdown!";
for($i = 10; $i >= 0; $i--)
{
echo $i."...";
}
echo "Blast Off!";
?>
Output:
Countdown!10… 9… 8… 7… 6… 5… 4… 3… 2… 1… 0… Blast Off!
We can also print out the counter variable to produce unique results. See you in the next tutorial on while loops.
Posted on 25. Feb, 2010 by admin in Basics
Up for another code challenge? Since the first code challenge we have covered string functions and conditions. Let’s see how much you know. Again, no cheating!
Q: Write a line of code combining two string variables, $var and $var2, into $var3.
Q: What is an escape character?
Q: List the 6 most important escape characters we discussed.
Q: Write a line of code that prints the length of this string literal, “How many cookies can I eat?”
Q: What does an else statement do?”
Q: What are 3 differences between if and switch statements?”
Project One: Write a program that reverses a string if it is more than 10 characters long.
Project Two: Write a program that checks if a string is a palindrome.