Tutorial
Click on thumbnailed images to enlarge
*Note: This tutorial is kind of advanced, so it's recommended that you have at least a basic knowledge of PHP. If not, then you may have difficulty understanding, but I tried to make this tutorial as understandable and easy to read as possible. If you are an advanced developer who works with PHP, then you may find some terms and vocabulary not fully, correctly, or exactly described; this is to help those who do not really understand it or have difficulties with it. This tutorial only applies to those who actually have websites and what not, only because you are not allowed to use PHP on myspace, createblog, and etc. I believe, however, that certain hosts that are free (possibly BlogSpot.com) will allow you to use PHP on your web page.
Basic: PHP Include
What are PHP Includes?
A PHP include is a PHP code that allows the webmaster to 'include', or display, the contents of another .PHP file. These are generally used to construct pages that have the exact same content, except this way is faster to update the content and not having to manually update the content in HTML on every page. PHP Includes are very useful if your site has many pages. Hell, even these forums use PHP includes! They can be very reliable and helpful.
How can I use them?
First of all, you will need the PHP include code. It's a rather short code so no worries at all really. The only requirements for the PHP include to work are that the file you are going to display, MUST be a .PHP file and the file where you have the PHP include MUST be a .PHP file as well. Below is the PHP include code.
*FILENAME is the name of the .PHP file you're going to display.
Intermediate: PHP Query Strings
What are PHP Query Strings?
A PHP Query String is a code that displays different variables with certain values in the URL bar due to the specified query values. In other words, it's the code that makes those nice little URL's like below:
Let's go over this URL in the next step.
What do the parts of the URL mean?
In the URL I displayed above as an example, there are some parts we should go over.
This is our domain name, the base URL:
This is the file path to the .PHP file where you are going to display the query.
This question mark after the .PHP extension states that there is going to be a query.
This is the defined query string. You have a variable that is like a book, and the value that displays the exact 'page' we want to see in the book.
This ampersand symbol comes after a query string and it states that there is going to be another query string come after the symbol.
This is just another query string, except with a different variable and a different value.
*Note: If you have two or more query strings (whether they have different variables and/or values or not), they are each separated by an ampersand symbol ( & ).
Now let's see what makes this nice URL.
How can I use them?
The PHP Query String code will usually need a $_GET method, because we are trying to recieve, and not submit (using the $_POST method). In the $_GET method, you will define the variable and then the IF statement will be used to display what you got from the $_GET method, if you got anything at all. If you did get something, it will look for the value we specified and defined in the code. Once that happens, we then use ECHO to 'echo' the contents of that certain variable and value combination. Once we're done defining the variables and existing values to that variable, we will need to add an ELSE statement to display the error page because if there is something else that we didn't define and doesn't exist, it won't display and will generate an error. The only requirements are that the file you are using the PHP Query String in MUST be a .PHP file. Below is a simple PHP Query String code:
Just define the variables and define the appropriate values. *Note: You cannot use a DOUBLE QUOTATION MARK ( " ) unless you add a backslash ( \ ) before it, so it would be this - ( \" ). You may, however, use a SINGLE QUOTATION MARK ( ' ) without the backslash. We do this because since we are using the ECHO statement, we already have two double quotation marks, and if use another two inside the ECHO statement, then it will corrupt the code because it interferes with the double quotation marks used for the ECHO statement to display the content. And yes, you can display HTML/CSS in your content, but remember, you have to use SINGLE QUOTATION MARKS ( ' ) or DOUBLE QUOTATION MARKS WITH A BACKSLASH BEFORE IT ( \" ).
To add another value for the variable just keep adding this, but make sure it doesn't come before the ELSE (error) statement and that it is different from all the other values:
BUT WAIT!?!?! What if you want to display multiple different variables in the URL bar? Simply, just add the below on the same line on another ELSE IF statement.
You add that in between the parentheses and you add the below in between the multiple different variables in the code:
So now the code would look like this:
Advanced: Using PHP Includes inside of PHP Query Strings
Why won't the PHP Include work?
So, what if you're trying to display the content of another page inside of your query string? You may have tried the below, right?
} else if ( $variable == 'value' ) {
echo "<?php include("FILENAME.PHP"); ?>";
}
You see, the problem here is that we already have the question marks to begin the PHP Query String code, so including two more will just cancel them, thus canceling the PHP Include. The answer is very simple actually.
Since we already have the question marks to begin and end the PHP Query String code, we can also use different statements inside of the ELSE IF statements, which mean we can simply use the INCLUDE statement! First off, make another .PHP file first, and whatever content you have in one of your specified values, place it into the new .PHP file. Now instead of using this:
Just use this:
Now, you see how in the first code, we have this line to display what happens if you go to variable=value1:
Then, you'll notice the content1.php I included in the second code; that will contain the content I had when I was using the ECHO statement. So content1.php will look like this:
Also, if you use this method, you don't have to add a backslash ( \ ) before any double or single quotation marks. You can also even use PHP includes in that new .PHP file.
This leaves me to the last part of the tutorial, how to include a query string. As I've said before, you can't use a another question mark for the query string you're trying to display; since there already are two question marks for the PHP include. Once again, we will use the awesome $_GET method! All you have to do is this:
*Note: you MUST place the $_GET method first for it to work.
* You can view a live example of the first PHP Query String code here.
* You can view a live example of the second PHP Query String code here. - *Take a look above in the tutorial where I included files called "homepage.php", "content1.php", etc. and replace the "query_example2.php" in the URL bar, with the file name of one of those file names I included above in the tutorial. ;)
Hope that wasn't too confusing and helpful! If you have any problems, questions, or concerns, feel free to PM me or comment on this tutorial. Thanks. :)
-Mikeplyts
site | myspace
Basic: PHP Include
What are PHP Includes?
A PHP include is a PHP code that allows the webmaster to 'include', or display, the contents of another .PHP file. These are generally used to construct pages that have the exact same content, except this way is faster to update the content and not having to manually update the content in HTML on every page. PHP Includes are very useful if your site has many pages. Hell, even these forums use PHP includes! They can be very reliable and helpful.
How can I use them?
First of all, you will need the PHP include code. It's a rather short code so no worries at all really. The only requirements for the PHP include to work are that the file you are going to display, MUST be a .PHP file and the file where you have the PHP include MUST be a .PHP file as well. Below is the PHP include code.
<?php
include("FILENAME.php");
?>
include("FILENAME.php");
?>
*FILENAME is the name of the .PHP file you're going to display.
Intermediate: PHP Query Strings
What are PHP Query Strings?
A PHP Query String is a code that displays different variables with certain values in the URL bar due to the specified query values. In other words, it's the code that makes those nice little URL's like below:
http://www.example.com/path/to/file.php?variable=value&diffvariable=diffvalue
Let's go over this URL in the next step.
What do the parts of the URL mean?
In the URL I displayed above as an example, there are some parts we should go over.
This is our domain name, the base URL:
http://www.example.com/
This is the file path to the .PHP file where you are going to display the query.
path/to/file.php
This question mark after the .PHP extension states that there is going to be a query.
?
This is the defined query string. You have a variable that is like a book, and the value that displays the exact 'page' we want to see in the book.
variable=value
This ampersand symbol comes after a query string and it states that there is going to be another query string come after the symbol.
&
This is just another query string, except with a different variable and a different value.
diffvariable=diffvalue
*Note: If you have two or more query strings (whether they have different variables and/or values or not), they are each separated by an ampersand symbol ( & ).
Now let's see what makes this nice URL.
How can I use them?
The PHP Query String code will usually need a $_GET method, because we are trying to recieve, and not submit (using the $_POST method). In the $_GET method, you will define the variable and then the IF statement will be used to display what you got from the $_GET method, if you got anything at all. If you did get something, it will look for the value we specified and defined in the code. Once that happens, we then use ECHO to 'echo' the contents of that certain variable and value combination. Once we're done defining the variables and existing values to that variable, we will need to add an ELSE statement to display the error page because if there is something else that we didn't define and doesn't exist, it won't display and will generate an error. The only requirements are that the file you are using the PHP Query String in MUST be a .PHP file. Below is a simple PHP Query String code:
<?php
if ( isset( $_GET['variable'] ) ){
$variable = $_GET['variable'];
}
if ( empty( $variable ) || $variable == '' || $variable == '0' ){
echo "Home (Index) Page Content.";
} else if ( $variable == 'value1' ){
echo "Content of Specified Value1";
} else if ( $variable == 'value2' ){
echo "Content of Specified Value2";
} else {
echo "Error Page Content.";
}
?>
if ( isset( $_GET['variable'] ) ){
$variable = $_GET['variable'];
}
if ( empty( $variable ) || $variable == '' || $variable == '0' ){
echo "Home (Index) Page Content.";
} else if ( $variable == 'value1' ){
echo "Content of Specified Value1";
} else if ( $variable == 'value2' ){
echo "Content of Specified Value2";
} else {
echo "Error Page Content.";
}
?>
Just define the variables and define the appropriate values. *Note: You cannot use a DOUBLE QUOTATION MARK ( " ) unless you add a backslash ( \ ) before it, so it would be this - ( \" ). You may, however, use a SINGLE QUOTATION MARK ( ' ) without the backslash. We do this because since we are using the ECHO statement, we already have two double quotation marks, and if use another two inside the ECHO statement, then it will corrupt the code because it interferes with the double quotation marks used for the ECHO statement to display the content. And yes, you can display HTML/CSS in your content, but remember, you have to use SINGLE QUOTATION MARKS ( ' ) or DOUBLE QUOTATION MARKS WITH A BACKSLASH BEFORE IT ( \" ).
To add another value for the variable just keep adding this, but make sure it doesn't come before the ELSE (error) statement and that it is different from all the other values:
else if ( $variable == 'value' ){
echo "Content of Specified Value";
}
echo "Content of Specified Value";
}
BUT WAIT!?!?! What if you want to display multiple different variables in the URL bar? Simply, just add the below on the same line on another ELSE IF statement.
$different_variable == 'different_value'
You add that in between the parentheses and you add the below in between the multiple different variables in the code:
||
So now the code would look like this:
<?php
if ( isset( $_GET['variable'] ) ){
$variable = $_GET['variable'];
}
if ( empty( $variable ) || $variable == '' || $variable == '0' ){
echo "Home (Index) Page Content.";
} else if ( $variable == 'value1' ){
echo "Content of Specified Value1";
} else if ( $variable == 'value2' ){
echo "Content of Specified Value2";
} else if ( $variable == 'value' || $different_variable == 'different_value' ){
echo "Content of Two Different Specified Values.";
} else {
echo "Error Page Content.";
}
?>
if ( isset( $_GET['variable'] ) ){
$variable = $_GET['variable'];
}
if ( empty( $variable ) || $variable == '' || $variable == '0' ){
echo "Home (Index) Page Content.";
} else if ( $variable == 'value1' ){
echo "Content of Specified Value1";
} else if ( $variable == 'value2' ){
echo "Content of Specified Value2";
} else if ( $variable == 'value' || $different_variable == 'different_value' ){
echo "Content of Two Different Specified Values.";
} else {
echo "Error Page Content.";
}
?>
Advanced: Using PHP Includes inside of PHP Query Strings
Why won't the PHP Include work?
So, what if you're trying to display the content of another page inside of your query string? You may have tried the below, right?
} else if ( $variable == 'value' ) {
echo "<?php include("FILENAME.PHP"); ?>";
}
You see, the problem here is that we already have the question marks to begin the PHP Query String code, so including two more will just cancel them, thus canceling the PHP Include. The answer is very simple actually.
Since we already have the question marks to begin and end the PHP Query String code, we can also use different statements inside of the ELSE IF statements, which mean we can simply use the INCLUDE statement! First off, make another .PHP file first, and whatever content you have in one of your specified values, place it into the new .PHP file. Now instead of using this:
<?php
if ( isset( $_GET['variable'] ) ){
$variable = $_GET['variable'];
}
if ( empty( $variable ) || $variable == '' || $variable == '0' ){
echo "Home (Index) Page Content.";
} else if ( $variable == 'value1' ){
echo "Content of Specified Value1";
} else if ( $variable == 'value2' ){
echo "Content of Specified Value2";
} else if ( $variable == 'value' || $different_variable == 'different_value' ){
echo "Content of Two Different Specified Values.";
} else {
echo "Error Page Content.";
}
?>
if ( isset( $_GET['variable'] ) ){
$variable = $_GET['variable'];
}
if ( empty( $variable ) || $variable == '' || $variable == '0' ){
echo "Home (Index) Page Content.";
} else if ( $variable == 'value1' ){
echo "Content of Specified Value1";
} else if ( $variable == 'value2' ){
echo "Content of Specified Value2";
} else if ( $variable == 'value' || $different_variable == 'different_value' ){
echo "Content of Two Different Specified Values.";
} else {
echo "Error Page Content.";
}
?>
Just use this:
<?php
if ( isset( $_GET['variable'] ) ){
$variable = $_GET['variable'];
}
if ( empty( $variable ) || $variable == '' || $variable == '0' ){
include("homepage.php");
} else if ( $variable == 'value1' ){
include("content1.php");
} else if ( $variable == 'value2' ){
include("content2.php");
} else if ( $variable == 'value' || $different_variable == 'different_value' ){
include("contents.php");
} else {
include("error.php");
}
?>
if ( isset( $_GET['variable'] ) ){
$variable = $_GET['variable'];
}
if ( empty( $variable ) || $variable == '' || $variable == '0' ){
include("homepage.php");
} else if ( $variable == 'value1' ){
include("content1.php");
} else if ( $variable == 'value2' ){
include("content2.php");
} else if ( $variable == 'value' || $different_variable == 'different_value' ){
include("contents.php");
} else {
include("error.php");
}
?>
Now, you see how in the first code, we have this line to display what happens if you go to variable=value1:
echo \"Content of Specified Value1\";
Then, you'll notice the content1.php I included in the second code; that will contain the content I had when I was using the ECHO statement. So content1.php will look like this:
Content of Specified Value1
Also, if you use this method, you don't have to add a backslash ( \ ) before any double or single quotation marks. You can also even use PHP includes in that new .PHP file.
This leaves me to the last part of the tutorial, how to include a query string. As I've said before, you can't use a another question mark for the query string you're trying to display; since there already are two question marks for the PHP include. Once again, we will use the awesome $_GET method! All you have to do is this:
<?php
$_GET['variable'] = 'value';
include("querypage.php");
?>
$_GET['variable'] = 'value';
include("querypage.php");
?>
*Note: you MUST place the $_GET method first for it to work.
* You can view a live example of the first PHP Query String code here.
* You can view a live example of the second PHP Query String code here. - *Take a look above in the tutorial where I included files called "homepage.php", "content1.php", etc. and replace the "query_example2.php" in the URL bar, with the file name of one of those file names I included above in the tutorial. ;)
Hope that wasn't too confusing and helpful! If you have any problems, questions, or concerns, feel free to PM me or comment on this tutorial. Thanks. :)
-Mikeplyts
site | myspace
Tutorial Comments
Showing latest 1 of 1 comments
Tutorial Details
| Author |
Mikeplyts
|
| Submitted on | Oct 16, 2009 |
| Page views | 1,996 |
| Favorites | 3 |
| Comments | 1 |
| Reviewer |
manny-the-dino
|
| Approved on | Oct 17, 2009 |