PHP
  Home arrow PHP arrow Page 5 - PHP 101 (Part 1) - Secret Agent Man
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP 101 (Part 1) - Secret Agent Man
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 34
    2000-07-31


    Table of Contents:
  • PHP 101 (Part 1) - Secret Agent Man
  • Bond...James Bond
  • A Case Of Identity
  • The Toy Shop
  • Weapons To Die For

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    PHP 101 (Part 1) - Secret Agent Man - Weapons To Die For
    ( Page 5 of 5 )

    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
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT