Basics of PHP
Other Uses of Variables
As you have seen, variables are as simple as defining them (either in the current file or in a file like vars1.php; you can define variables and use them in any PHP file) and then calling them up where you want them used. There are many other uses for variables other than just with the header and footer of your pages.
Say you have an image that you want to use on multiple pages and you want it positioned just so on each page. A dividing bar, for example, that you want between paragraphs. Instead of having to put static code on every page where you want the image, you could put it in a variable in a file that is included on every page. Then, just echo the variable:
Place this somewhere in vars1.php:
<p style=\"text-align:center;\">
<img src=\"images/horizontal_bar.png\" alt=\"bar\" width=\"200\" height=\"2\">
</p>
";
Place this wherever you want the image to show up:
Switches and Cases
It may still seem too repititious placing include "vars1.php";, echo $head;, and echo $foot; on each page. What if you were to change the name of vars1.php to variables.php? What if you wanted to add some other bit of code to the top of each page, but for some logical reason, you didn't want to place that code into the variable $head? If you were to do such things, you would have to go through each page that the code was on and update it. That's almost like using straight HTML!
Now, you may think that you will never want to change the name of your vars1.php file, or that $head and $foot are good enough for anything to be put into them. Do not think this way. You should always leave room for future expansion and, most importantly, the future complication of your site.
Using a switch and several cases is a handy way to get the same bit of code on the top of each page without actually putting things like include "vars1.php";, echo $head;, and echo $foot; into each individual file.
- If you do not already have one, create a file called index.php. If the index page of your site is currently a .html, .asp, .htm, or any other extension besides .php, you will need to delete that file or rename it to have a .php extension.
- In index.php, put the following:
include "vars1.php";
echo $head;
$page_choice = $_GET['load'];
switch($page_choice) {
case "me": include "aboutme.php"; break;
case "links": include "links.php"; break;
default: echo "
Put your actual content here. You will need to escape quotes.
";
}
echo $foot;
?>
That looks kind of complex, doesn't it? Don't worry about it! It's really simple to work with. You have probably used a switch and cases on other sites before and just haven't realized it. Have you ever encountered a URL like this one?: http://www.site.com/index.php?choice=frog If so, then you have seen the results of a switch and a case.
The basic idea here is switching out what the visitor sees based on the URL to which he or she goes. Whatever is inside the default: section is what is seen when that actual page is viewed. Here, if a person went to index.php, they would see whatever is in $head, the message “Put your actual content here,” and the contents of $foot.
You can see that there are two cases listed: “me” and “links.” Inside each of those cases, there is a file included. For the case “me,” the file aboutme.php is included. For the case “links,” the file links.php is included. Now, if the visitor were to load the case “me,” instead of seeing the message in the default case, they would see the contents of aboutme.php, but the contents of $head and $foot would still show up because they are not within the default case.
Let's configure your links.php file with the following code:
<?php echo $divbar; ?>
<a href="http://www.yahoo.com">Yahoo</a>
That's all you need to put into links.php. You don't need to put <html> or any CSS or </body> or any of that stuff. All of that coding is in your $head and $foot variables which are echoed around the switch that contains the case "links."
You need to be able to access those cases you have defined in your switch.
If you want to make a link to your About Me page, you would do the following:
<a href="index.php?load=me">About Me</a>
To access your Links page, use the following link:
<a href="index.php?load=links">Favorite Sites</a>
Breaking the URL Down
- index.php - You point the link to index.php because that is the file with the switch/cases in it.
- ?load - This tells the server which switch it is working with. If you noticed, there was the following line in the code of your index.php: $page_choice = $_GET['load']; You can change the word “load” to anything you like. I would recommend a fairly short word; a lot of people use a simple x. If you did this, that line would need to be like this: $page_choice = $_GET['x']; and a URL would look like this: <a href="index.php?x=links">Favorite Sites</a>
- =links - This tells the server which case to load.
Basically, here is the breakdown of a URL to a specific case in a specific switch:
file_with_switch_and_cases.php?the_switch=the_case
Combining All of This
You don't have to copy and paste all of the code I have shown you and just use it as-is, verbatim. Change it! Tinker with it! See what works and what doesn't! The following are some alternate ways to do things:
In your index.php file:
<?php
include "vars1.php";
echo $head;
$page_choice = $_GET['page'];
switch($page_choice) {
case "site": include "about.php"; break;
case "friends": include "my_friends.html"; break;
default: echo $welcome_message;
}
echo $foot;
?>
Here, instead of echoing a bunch of stuff directly in the file, we are echoing
the variable $welcome_message. You will need to define this variable in your
vars1.php with something like this:
$welcome_message = "Hello! Welcome to my site!";
Multiple Switches
So you have your switch on your index page that loads all of your main content. Say you have multiple pages of things that pertain to you, though. You don't want to include all of those in cases on your index page, so just include one main file, say me.php, on the index page. On me.php, put another switch with different cases:
In your me.php file:
<?php
$page_choice = $_GET['moi'];
switch($page_choice) {
case "friends": include "me/friends.php"; break;
case "likes": include "me/likes.php"; break;
case "dislikes": include "me/dislikes.php"; break;
default: echo "
This is my biography.
";
}
?>
As you can see, I did not need to include $head or $foot because they are
on index.php and, when the case "me" is loaded from index.php, $head
and $foot will show up. To load one of the cases on me.php, use the following
code:
<a href="index.php?load=me&moi=likes">What I Like</a>
Cases
With cases, you are not limited to doing just one thing, such as echoing a variable or including a file. Look at the following examples to see what I mean:
$page_choice = $_GET['stuff'];
switch($page_choice) {
case "whee":
echo "Hello there.";
echo $divbar;
include "whee.php";
include "guess.txt";
echo $divbar;
break;
case "yay":
$hello = "Hey there!";
echo $hello;
include "hello.html";
default: echo $default;
}
So long as you put all the code you want in a case before the break; line, it will be executed when that case is called. Note that the default section does not need a break; line.
Sources / For More Information
- PHP.Net
http://www.php.net
In-depth online resource detailing PHP syntax as well as tips and tricks about using PHP