ECHOing more that one Variable at a time
You can use the ECHO command we learned earlier to display more than one variable at a time. Combining variables can be extremely useful. Take a look at this example...
<?php
$phrase1 = "That's No Moon,";
$phrase2 = "It's a Space Station!";
echo "$phrase1 $phrase2";
?>
This code example will show up in your web browser like this:
[IMG]file:///C:/Users/chris_2/AppData/Local/Temp/moz-screenshot-1.jpg[/IMG] That's No Moon, It's a Space Station!
[IMG]file:///C:/Users/chris_2/AppData/Local/Temp/moz-screenshot.jpg[/IMG]
Neat, huh?
So, you can see where this might be useful, I hope?
You can also echo text and variables in the same statement by putting periods around the variable....like so...
<?php
$items = 3;
echo "You have purchased ".$items." items.";
?>
It will display:
You have purchased 3 items.
PHP Includes
A PHP include is used when you want to include the contents of one file inside another...a very useful command!
<?php
include("file.inc");
?>
Real World Usage for Includes
Let’s say you are developing a 5 page website that you might be adding pages to. Your navigation HTML looks like this:
<html>
<head>
<title>My Navigation</title>
</head>
<body>
<a href=http://www.mysite.com/index.php>Home</a>
<a href=http://www.mysite.com/products.php>Products</a>
<a href=http://www.mysite.com/articles.php>Articles</a>
<a href=http://www.mysite.com/blog.php>Blog</a>
<a href=http://www.mysite.com/contact.php>Contact Us</a>
</body>
</html>
...and this is going to be in every page. Now, when you add a page to your website, you are going to have to change the code on all 5 pages to reflect the new link. This could take several minutes to do...and what if the site expands to 20 pages or more? It’s going to be a nightmare!
PHP Includes to the rescue!
Turn the page and watch PHP save our developer from certain doom!
Let’s cut the menu link code out of the example above and paste it into a new plain text document (You can use Notepad on Windows or TextEdit on the Mac for this).
<a href=http://www.mysite.com/index.php>Home</a>
<a href=http://www.mysite.com/products.php>Products</a>
<a href=http://www.mysite.com/articles.php>Articles</a>
<a href=http://www.mysite.com/blog.php>Blog</a>
<a href=http://www.mysite.com/contact.php>Contact Us</a>
Save this text file as navigation.inc. Paste the following in place of where the code was in the original HTML files...
<?php
include "navigation.inc" ;
?>
Then save the HTML page as a PHP page. Voila! Any time you need to change the menu links, all you have to do is edit one file -
the navigation.inc file!
A Note on Includes...
You don’t have to use .inc as the extension for an include -you can include almost any type of file with a PHP include - HTML, PHP, even other URL’s! One thing to keep in mind, however...is to strip out all the formatting code from the page you are including from (like the <html> and <body> tags). In other words, only include exactly what you need!
Bookmarks