Forum rules
Please read our Guide to Making Ozzu Tutorials if you would like to submit your own tutorials.
TUTORIAL: Learning PHP (Part 3)
Learning PHP - Part 3
This tutorial assumes that you know everything tought in the first two previous tutorials on this topic in this series (Part 1 and Part 2)
This tutorial will increase in the different ways you can use PHP and what you can do with it. You will be learn better ways and different ways to make validations for a form.
In Learning PHP (Part 1) you learned what PHP is and what you could do with it. You learned how to start using them and how to set and echo variables and then passing them from page to page with simple validation. You learned how to hide PHP until the submit button was pressed and how to either include or require a file to another file.
Part 1 in review
In Learning PHP (Part 2) you learned what strings are and when to use them. You also learned some operators that you can use with if...elseif...else statements or just to do some math. You also learned how to get HTML away from submitted data if you don't want them to submit HTML and resulting in parsing HTML that you don't want to.
Part 2 in review
The loops become very handy later on when you want to print a lot of information that are stored in a database. Loops are loops that go and go... repeat and repeat until either the code tells it to stop, when it reaches the limited cache memory or when it reaches the end of information it was told to go through.
There are a a number of loops available for a programmer to use. Some of them are while, do-while, for... and more, all for a different purpose and all work different ways and do different things. I'll show you some of them in this tutorial. The while loop and the for loop.
An example of the while loop is..
The above would echo the following values.
All the while loop is saying, if $t is less than or equal to 100 than continue the process. This means, that it will keep adding 10 to the previous value until it reaches 100. Than, when it reaches 100 it will stop the loop. You can manually tell the loop to stop, like the following example.
The above example would break out of the loop (stop the loop) when $t reaches 50. This means, that it will not show 50. It will stop at 40.
If you really need to, you can have a loop within a loop. Like the following example.
The for loop says, for $i as a and $i less than 10 and $i is always increasing, increase the $i until it reaches 9 (9 is the closes full integer you can use to get to 10). The script would repeat that until it is done.
The reason the $i is set to check if it's equal to 9 in the if statement, is because the $i is supposed to be less than 10, so it will never actually be 10.
The above script will output the following values.
As you see, the values "1, 2, 3, 4, 5, 6, 7, 8, 9, 0" come after each of the "10, 20, 30, 40, 50, 60, 70, 80, 90". That is because the for loop that is inside the while loop, is after the echo (echo $t ."<br />\n";), so the script is printing the values of while loop before the for loop. Than when all is done, it repeats until the while loop has reached it's ending point.
You can do a bit with arrays. There are different types of arrays, and each array you call differently. Below is one of the easier arrays called "Numeric Array".
Let's put some people in the array.
Let's call on the information from that array we would use the following code in the following example.
The above echo statement would say "Peter is Joel's friend". Notice that the number in between the square brackets " [...] " are in order as they appear in the array (zero [0] being the first entry) so it shows in the text in order. You don't have to have them in order. You can put 0 and than 3 or 3 and then 2 or whatever you need/want.
In comparison, arrays work nearly the same way as variables do... instead there is more functionality for it. In this case, the comma seperates the variables from each other.
"Associative arrays". They are called that becuase you associate certain names for them to recal them.
Let's create an array that stores some people's ages.
Let's now extract the information from the stored "variables" in the array.
Notice that the name of the variable ($variable) is used to echo the information, and the text in quotes " "..." " are using in between single quotes which are in between square brackets " ['...'] ".
The same goes for the other ones. The double arrows in the array store the pointed information into the text that the arrow leads from. In comparison, arrays work nearly the same way as variables do... instead there is more functionality for it. In this case, the comma seperates the variables from each other.
There is a bit harder type of an array that we can use that increases functionality. This type of an array is called "Multidimensional array". In this type of an array, you are storing an array within an array.
You can make menus that use "<ul>...</ul>" options for graduation such as the following example (Multidimensional array).
There are two ways that I know of that you can call the variables from this array. You can't really call on a variable that holds an array, otherwise you will get the text "array" and not what that array holds. You need to call on a variable that holds information. In the example above the the text That are in between the quotes and commas after the quotes are the information. One easy way to call the variables from the information stored in the array is shown below.
The above example would print "Echoes 4". Notice how the names of the variables that store arrays are in "Associative array" style. Meaning you call on them by their names and that they are in order that they are in the $variable array. That is important as you don't want to mess up the code. But if you do do that, than you will not see anything.
Also notice the numeric value " [3] " in that echo statement. The numeric values calls on the information that are stored in the array. You can store an array into variables... any named variables that you want to name.
There is a shorthand for arrays as well... You can shorthand associative arrays and numeric arrays using this method...
You can name them with any name you want (The names go between the single quotes which are in between the square brackets " ['...'] "). You echo them just as you will echo any variable.
There are also a different and a bit easier ways to print the arrays out on the screen. And that is to use loops such as foreach or for.
There are also a different and a bit easier ways to print the arrays out on the screen. And that is to use loops such as foreach.
If we implement the foreach for the big navigation ARRAY above, it would look like...
The above would look like the following in the browser.
The above code is much better than echoing the navigational links without loops. Like the following short example.
See, how easier it is to use a loop in this instance? Yeah, loops help in a lot more cases than this.
Now, all you have to do, is put the navigational links in the PHP code and you got yourself a navigation. If you really want to, you can change it to a list using a <ul>...</ul> or an <ol>...</ol>. Even though, most programmers don't use this way to create navigational links, it's one way of doing it.
If you have a form that you need multiple validations of (such as validating a username and password) and want to do something if the form was submitted correctly but do something else if there were errors in the form, than you can do a couple of techniques shown in this tutorial.
There are a couple of ways, one way is to put the errors in an array and count the error arrays. If there are at least 1 error array than the form would validate as bad, otherwise, the form would validate as good.
There is a useful function that allows use to count the number of objects that we have in the code. The function is count(...);. In place of '...', we put the name of the object that we want to count.
As you see, in the above validation, we are counting the amount of $error arrays we have in the form, and if there is at least 1, than it would be validation in the if...else... statement as bad submittal, otherwise, it would be validated as good.
There are some other ways you can do that without using arrays. An example of one of those ways, is shown below. That way, is to validate one thing at a time, and if one thing is under bad, it would not go to another thing. Basically, we are validating one thing inside of another "good" validation.
In the following example, we are going to make the strlent($name} validation in a different if...else... statement.
In the above example, we are "catching" the errors and and if there are any errors, we are stopping the validation. If you think about this logically, it is merely saying the following:
If the $name variable is empty, echo "You need to fill in the input field with your name." else if the $name is greater than 15 characters than echo "Your name is too big. Make sure it's not greater than 15 characters." else echo "Your name is $name".
If one of those "if" are met, than the script will not go to the "else" as there is no else if the "if" is true.
In conclusion, you learned different ways of validation forms using arrays and loops. You also learned how to setup the arrays and loops correctly, and saw hot they work.
<- Go to part 2 || part 4 is in creation
If you have any questions or suggestions, please PM me
Introduction
This tutorial assumes that you know everything tought in the first two previous tutorials on this topic in this series (Part 1 and Part 2)
This tutorial will increase in the different ways you can use PHP and what you can do with it. You will be learn better ways and different ways to make validations for a form.
A Look Back
In Learning PHP (Part 1) you learned what PHP is and what you could do with it. You learned how to start using them and how to set and echo variables and then passing them from page to page with simple validation. You learned how to hide PHP until the submit button was pressed and how to either include or require a file to another file.
Part 1 in review
Code: Select all
- <?php
- //<?php starts the PHP parsing
- //This are short comments
- /* This are very long comments
- that can go from line to line
- as much as you want it to go */
- //Setting a variable with some text
- $variable = 'This variable is set with this text';
- //Echoing the above variable
- echo $variable;
- //echoing some text as string
- echo 'This is a string being echoed';
- //Here is where we need to escape a single quote
- echo 'Isn\'t this easy?';
- //A simple if...elseif...else statement
- if(empty($variable))
- {
- echo 'The variable is not set';
- } elseif($variable = 'This variable is set with this text') {
- echo $variable;
- } else {
- echo 'Are you talking to me?';
- }
- //The above if...elseif...else statement would print the echo under the elseif
- Here we are including google
- include('http://www.google.com');
- //The ?> closes the PHP parsing
- ?>
In Learning PHP (Part 2) you learned what strings are and when to use them. You also learned some operators that you can use with if...elseif...else statements or just to do some math. You also learned how to get HTML away from submitted data if you don't want them to submit HTML and resulting in parsing HTML that you don't want to.
Part 2 in review
Code: Select all
- <?php
- //The following is a string
- $echo = 'This is a string';
- //The following is not a string;
- echo $echo;
- //We are doing some checking below about the variable $echo
- if($echo == 'This is a string')
- {
- echo $echo;
- } elseif($echo != 'This is a string') {
- echo '$echo is not "This is a string"';
- } else {
- echo '$echo is not "This is a string" times 2';
- }
- //Setting a variable submitted from a form
- $submittedData = $_POST['submitted'];
- //We are stripping the tag from HTML
- $_POST = array_map('strip_tags', $_POST);
- ?>
Loops
The loops become very handy later on when you want to print a lot of information that are stored in a database. Loops are loops that go and go... repeat and repeat until either the code tells it to stop, when it reaches the limited cache memory or when it reaches the end of information it was told to go through.
There are a a number of loops available for a programmer to use. Some of them are while, do-while, for... and more, all for a different purpose and all work different ways and do different things. I'll show you some of them in this tutorial. The while loop and the for loop.
An example of the while loop is..
Code: Select all
- <?php
- //Setting the value
- $t = 10;
- //The while loop
- while ( $t <= 100 ) {
- //echoing $t
- echo $t ."<br />\n";
- //reinstating the $t
- $t = $t + 10 ."\n";
- }
- ?>
The above would echo the following values.
Above While Loop Example wrote:
10
20
30
40
50
60
70
80
90
100
20
30
40
50
60
70
80
90
100
All the while loop is saying, if $t is less than or equal to 100 than continue the process. This means, that it will keep adding 10 to the previous value until it reaches 100. Than, when it reaches 100 it will stop the loop. You can manually tell the loop to stop, like the following example.
Code: Select all
- <?php
- //Setting the value
- $t = 10;
- //The while loop
- while ( $t <= 100 ) {
- //echoing $t
- echo $t ."<br />\n";
- //reinstating the $t
- $t = $t + 10 ."\n";
- if($t == 50)
- {
- break;
- }
- }
- ?>
The above example would break out of the loop (stop the loop) when $t reaches 50. This means, that it will not show 50. It will stop at 40.
If you really need to, you can have a loop within a loop. Like the following example.
Code: Select all
- <?php
- //Setting the value
- $t = 10;
- //The while and for loop
- while($t <= 100)
- {
- echo $t ."<br />\n";
- $t = $t + 10 ."\n";
- for($i = 1; $i < 10; $i++)
- {
- echo $i .", ;
- if($i == 9)
- {
- echo '0<br />'."\n";
- }
- }
- }
- ?>
The for loop says, for $i as a and $i less than 10 and $i is always increasing, increase the $i until it reaches 9 (9 is the closes full integer you can use to get to 10). The script would repeat that until it is done.
The reason the $i is set to check if it's equal to 9 in the if statement, is because the $i is supposed to be less than 10, so it will never actually be 10.
The above script will output the following values.
Above while and for loops wrote:
10
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
20
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
30
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
40
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
50
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
60
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
70
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
80
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
90
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
100
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
20
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
30
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
40
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
50
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
60
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
70
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
80
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
90
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
100
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
As you see, the values "1, 2, 3, 4, 5, 6, 7, 8, 9, 0" come after each of the "10, 20, 30, 40, 50, 60, 70, 80, 90". That is because the for loop that is inside the while loop, is after the echo (echo $t ."<br />\n";), so the script is printing the values of while loop before the for loop. Than when all is done, it repeats until the while loop has reached it's ending point.
Arrays
You can do a bit with arrays. There are different types of arrays, and each array you call differently. Below is one of the easier arrays called "Numeric Array".
Let's put some people in the array.
Let's call on the information from that array we would use the following code in the following example.
The above echo statement would say "Peter is Joel's friend". Notice that the number in between the square brackets " [...] " are in order as they appear in the array (zero [0] being the first entry) so it shows in the text in order. You don't have to have them in order. You can put 0 and than 3 or 3 and then 2 or whatever you need/want.
In comparison, arrays work nearly the same way as variables do... instead there is more functionality for it. In this case, the comma seperates the variables from each other.
"Associative arrays". They are called that becuase you associate certain names for them to recal them.
Let's create an array that stores some people's ages.
Let's now extract the information from the stored "variables" in the array.
Code: Select all
- <?php
- //We are calling Peter's age
- echo 'Peter is '. $variable['Peter'] .' years old';
- ?>
Notice that the name of the variable ($variable) is used to echo the information, and the text in quotes " "..." " are using in between single quotes which are in between square brackets " ['...'] ".
The same goes for the other ones. The double arrows in the array store the pointed information into the text that the arrow leads from. In comparison, arrays work nearly the same way as variables do... instead there is more functionality for it. In this case, the comma seperates the variables from each other.
There is a bit harder type of an array that we can use that increases functionality. This type of an array is called "Multidimensional array". In this type of an array, you are storing an array within an array.
You can make menus that use "<ul>...</ul>" options for graduation such as the following example (Multidimensional array).
Code: Select all
- <?php
- $variable = array
- (
- "Tutorials" => array
- (
- "PHP" => array
- (
- "ARRAYS" => array
- (
- "Arrays 1",
- "Arrays 2",
- "Arrays 3",
- "Arrays 4",
- "Arrays 5"
- ),
- "ECHOES" => array
- (
- "Echoes 1",
- "Echoes 2",
- "Echoes 3",
- "Echoes 4",
- "Echoes 5"
- ),
- ),
- "HTML" => array
- (
- "CSS",
- "DEVELOPMENT",
- "TIPS AND TRICKS",
- "DIVS",
- "SPANS",
- "VALIDITY"
- )
- )
- );
- ?>
There are two ways that I know of that you can call the variables from this array. You can't really call on a variable that holds an array, otherwise you will get the text "array" and not what that array holds. You need to call on a variable that holds information. In the example above the the text That are in between the quotes and commas after the quotes are the information. One easy way to call the variables from the information stored in the array is shown below.
The above example would print "Echoes 4". Notice how the names of the variables that store arrays are in "Associative array" style. Meaning you call on them by their names and that they are in order that they are in the $variable array. That is important as you don't want to mess up the code. But if you do do that, than you will not see anything.
Also notice the numeric value " [3] " in that echo statement. The numeric values calls on the information that are stored in the array. You can store an array into variables... any named variables that you want to name.
There is a shorthand for arrays as well... You can shorthand associative arrays and numeric arrays using this method...
Code: Select all
- <?php
- //Associative Array Shorthand Method
- $name['Peter'] = '32';
- $name['Joel'] = '26';
- $name['Abraham'] = '43';
- //Numeric Array Shorthand Method
- $name['0'] = 'Peter';
- $name['1'] = 'Joel';
- $name['2'] = 'Abraham';
- ?>
You can name them with any name you want (The names go between the single quotes which are in between the square brackets " ['...'] "). You echo them just as you will echo any variable.
There are also a different and a bit easier ways to print the arrays out on the screen. And that is to use loops such as foreach or for.
There are also a different and a bit easier ways to print the arrays out on the screen. And that is to use loops such as foreach.
If we implement the foreach for the big navigation ARRAY above, it would look like...
Code: Select all
- <?php
- //Setting the array
- $variable = array
- (
- "Tutorials" => array
- (
- "PHP" => array
- (
- "ARRAYS" => array
- (
- "Arrays 1",
- "Arrays 2",
- "Arrays 3",
- "Arrays 4",
- "Arrays 5"
- ),
- "ECHOES" => array
- (
- "Echoes 1",
- "Echoes 2",
- "Echoes 3",
- "Echoes 4",
- "Echoes 5"
- ),
- ),
- "HTML" => array
- (
- "CSS",
- "DEVELOPMENT",
- "TIPS AND TRICKS",
- "DIVS",
- "SPANS",
- "VALIDITY"
- )
- )
- );
- //Echoing the navigational stuff
- echo 'Tutorials<br />'."\n";
- echo ' PHP Tutorials<br />'."\n";
- echo ' ARRAYS<br />'."\n";
- //foreach loop to display the data
- foreach($variable['Tutorials']['PHP']['ARRAYS'] as $value)
- {
- echo ' '. $value .'<br />'."\n";
- }
- echo ' ECHOES<br />'."\n";
- //foreach loop to display the data
- foreach($variable['Tutorials']['PHP']['ECHOES'] as $value)
- {
- echo ' '. $value .'<br />'."\n";
- }
- echo ' HTML Tutorials<br />'."\n";
- //foreach loop to display the data
- foreach($variable['Tutorials']['HTML'] as $value)
- {
- echo ' '. $value .'<br />'."\n";
- }
- ?>
The above would look like the following in the browser.
Code: Select all
- Tutorials
- PHP Tutorials
- ARRAYS
- Arrays 1
- Arrays 2
- Arrays 3
- Arrays 4
- Arrays 5
- ECHOES
- Echoes 1
- Echoes 2
- Echoes 3
- Echoes 4
- Echoes 5
- HTML Tutorials
- CSS
- DEVELOPMENT
- TIPS AND TRICKS
- DIVS
- SPANS
- VALIDITY
The above code is much better than echoing the navigational links without loops. Like the following short example.
Code: Select all
- <?php
- //snipped the navigational array
- //Echoing the navigational stuff
- echo 'Tutorials<br />'."\n";
- echo ' PHP Tutorials<br />'."\n";
- echo ' ARRAYS<br />'."\n";
- echo ' $variable['Tutorials']['PHP']['ECHOES']['0'] ."<br />\n";
- echo ' $variable['Tutorials']['PHP']['ECHOES']['1'] ."<br />\n";
- echo ' $variable['Tutorials']['PHP']['ECHOES']['2'] ."<br />\n";
- //Snipped... you get the picture
- ?>
See, how easier it is to use a loop in this instance? Yeah, loops help in a lot more cases than this.
Now, all you have to do, is put the navigational links in the PHP code and you got yourself a navigation. If you really want to, you can change it to a list using a <ul>...</ul> or an <ol>...</ol>. Even though, most programmers don't use this way to create navigational links, it's one way of doing it.
Different Ways Of Validating A Form
If you have a form that you need multiple validations of (such as validating a username and password) and want to do something if the form was submitted correctly but do something else if there were errors in the form, than you can do a couple of techniques shown in this tutorial.
There are a couple of ways, one way is to put the errors in an array and count the error arrays. If there are at least 1 error array than the form would validate as bad, otherwise, the form would validate as good.
There is a useful function that allows use to count the number of objects that we have in the code. The function is count(...);. In place of '...', we put the name of the object that we want to count.
Code: Select all
- <?php
- if(isset($_POST['submit']))
- {
- //Stripping submitted values from HTML
- $_POST = array_map('strip_tags', $_POST);
- //Setting the variables
- $name = $_POST['Pname'];
- //Checking if the input field has being filled in
- if(empty($name))
- {
- $error[] = 'You need to fill in the input field with your name.<br />';
- } elseif(strlen($name) > '15') {
- $error[] = 'Your name is too big. Make sure it is only 15 characters long.<br />';
- } else {
- echo 'Your name is: '. $name .'<br />';
- }
- //Implementing the count(...) functioin in our use to count the number of $error[] arrays
- if(count($error) > '0')
- {
- //There are more than 0 $error arrays, echo the validation for bad submittal
- echo 'You made a number of mistakes in your form. Please fix them'."\n";
- } else {
- //There were 0 $error errays, meaning that the form was filled in correctly. Do the below.
- echo '<b>Some bold and <u>underlined and <i>italicized text</i></u></b>.<br />';
- echo '<i>Welcome to this universe '. $name .'</i><br />';
- echo '<p><a href="#">Continue...</a></p>'."\n";
- }
- }
- ?>
As you see, in the above validation, we are counting the amount of $error arrays we have in the form, and if there is at least 1, than it would be validation in the if...else... statement as bad submittal, otherwise, it would be validated as good.
There are some other ways you can do that without using arrays. An example of one of those ways, is shown below. That way, is to validate one thing at a time, and if one thing is under bad, it would not go to another thing. Basically, we are validating one thing inside of another "good" validation.
In the following example, we are going to make the strlent($name} validation in a different if...else... statement.
Code: Select all
- <?php
- if(isset($_POST['submit']))
- {
- //Stripping submitted values from HTML
- $_POST = array_map('strip_tags', $_POST);
- //Setting the variables
- $name = $_POST['Pname'];
- //Checking if the input field has being filled in
- if(empty($name))
- {
- //"bad" validation
- echo 'You need to fill in the input field with your name.<br />';
- } else{
- //"good" validation... meaning that the $name is NOT empty
- if(strlen($name) > '15')
- {
- //"bad" validation. Meaning the $name is too big (Bigger that 14 characters
- echo 'Your name is too big. Make sure it's not greater than 15 characters.';
- } else {
- //"good" validation. And as it's the last validation we are doing, we are printing the results.
- echo 'Your name is: '. $name .'<br />';
- }
- }
- }
- ?>
In the above example, we are "catching" the errors and and if there are any errors, we are stopping the validation. If you think about this logically, it is merely saying the following:
If the $name variable is empty, echo "You need to fill in the input field with your name." else if the $name is greater than 15 characters than echo "Your name is too big. Make sure it's not greater than 15 characters." else echo "Your name is $name".
If one of those "if" are met, than the script will not go to the "else" as there is no else if the "if" is true.
Conclusion
In conclusion, you learned different ways of validation forms using arrays and loops. You also learned how to setup the arrays and loops correctly, and saw hot they work.
<- Go to part 2 || part 4 is in creation
If you have any questions or suggestions, please PM me
Guys, I need help with Wedevoy.com here. Thanks.
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
April 26th, 2008, 4:07 pm
Bogey, Your
should fix my problem with students_exam :
But i can't use it with MySQL queries.
Thx
should fix my problem with students_exam :
But i can't use it with MySQL queries.
Thx
Page 1 of 1
To Reply to this topic you need LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 2 posts
- Moderator: Moderator Team
- Users browsing this forum: No registered users and 1 guest
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum


