Home arrow PHP arrow Page 5 - PHP 101 (Part 1) - Secret Agent Man

Weapons To Die For - PHP

PHP is the hottest scripting language around - and with the release of PHP4, more and more developers are looking at it as a rapid Web development tool. This new series of tutorials is aimed at getting novice programmers up to speed on the language, and the first article covers variables, operators and the include() function call. Exploding chewing-gum is optional.

TABLE OF CONTENTS:
  1. PHP 101 (Part 1) - Secret Agent Man
  2. Bond...James Bond
  3. A Case Of Identity
  4. The Toy Shop
  5. Weapons To Die For
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 36
July 31, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
If you've used C before, you're probably already familiar with the "include" directive that appears near the beginning of every C program. PHP supports two functions which have a similar job to do - the include() function and the require() function. Take a look at a simple example:

<html> <head> <title>Weapons Worth Dying For</title> <style> h1,h3,li { font-family:Verdana; } </style> </head> <?php // this time, Bond's going to need the cigarette-lighter gun... require("./gun.php4"); // the new car-copter... include("./car.php4"); // and of course, the gold watch with the hidden GPS locator require("./watch.php4"); ?> <body> <h3>So, James, here's your check list for the mission.</h3> <ol type="a"> <li>The gun: <?php echo "$gun"; ?> <li>The car: <?php echo "$car"; ?> <li>The watch: <?php echo "$watch"; ?> </ol> <br> <h3>Oh yes...and remember never to let them see you cry. Good luck, 007!</h3> </body> </html>
Now, if you try to access this page as it, you'll get a bunch of error messages warning you about missing files. So you need to create the files "gun.php4", "car.php4" and "watch.php4":

[gun.php4]


<?php $gun = "AK-47"; ?>
[car.php4]

<?php $car = "BMW G8"; ?>
[watch.php4]

<?php $watch = "Rolex SAW-007"; ?>
And this time, when you access the primary page, PHP should automatically include the specified files, read the variables $gun, $watch and $car from them, and display them on the page.

A quick note on the difference between the include() and require() functions - the require() function is always replaced by the contents of the file it points to, and therefore cannot be used in a conditional statement ["if this is true, require that file"] since the file will be read in regardless. However, the include() function allows you to optionally include or exclude files on the basis of a conditional test. Also, a require()d file cannot return values to the main PHP script, while an included file can.

An important point to be noted is that when a file is require()d or include()d, the PHP parser leaves "PHP mode" and goes back to regular "HTML mode". Therefore, all PHP code within the included external files needs to be enclosed within regular PHP <?...?> tags.

A very useful and practical application of the include() function is to use it to include a standard footer or copyright notice across all the pages of your Web site, like this:


<html> <head> <title></title> </head> <body> ...your HTML page... <br> <? include("footer.html"); ?> </body> </html>
where "footer.html" contains

<font size=-1 face=Arial>This material copyright Melonfire, 2000. All rights reserved.</font>

Now, this footer will appear on each and every page that contains the include() statement above - and, if you need to change the message, you only need to edit the single file named "footer.html"!

And that's about it for this week. We've shown you the basic building blocks of PHP - its variables and operators - and next time, we'll be using those fundamental concepts to demonstrate PHP's powerful form processing capabilities. Don't miss it!

 
 
>>> More PHP Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: