p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: prepend.inc


Message #1 by "Allan Stuntz" <arsrn51@u...> on Fri, 22 Feb 2002 22:47:07
With appologies to all the really good programmers, could someone show me 

an example of "prepend.inc"? It is used on pg 541 of Por PHP. What I am 

trying to do is use this so that I can put my header and footer in one 

place rather than make changes to each individual page. Any help/advice 

appreciated.

-Allan
Message #2 by "Nikolai Devereaux" <yomama@u...> on Fri, 22 Feb 2002 15:07:01 -0800

I don't have the book in front of me, so this is purely my assumption of

what you want:



-- prepend.inc --

<?

function page_header($title)

{

    if ($title == "")

        $title = "Your Default Title Here";



    $str = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "

         . "HTML 4.0 Transitional//EN\">\n"

         . "<html>\n<head><title>$title</title></head>\n"

         . "<body>\n";

}



function page_footer()

{

    echo "</body>\n</html>"; // or whatever else you want here.

}

?>

-----------------





-- any other php page --

<?

    require_once("prepend.inc");



    page_header("This is my title");



    ... all your code here.



    page_footer();

?>

---------------------





Does that make sense?  I prefer require_once or include_once over their

non-_once versions unless I specifically WANT the file to be inserted and

parsed multiple times.  It's much more efficient to use the _once versions

(especially if your include files are long or just function definitions)

because the interpreter doesn't have to parse through them unnecessarily.



take care,



nik





> -----Original Message-----

> From: Allan Stuntz [mailto:arsrn51@u...]

> Sent: Friday, February 22, 2002 10:47 PM

> To: beginning php

> Subject: [beginning_php] prepend.inc

>

>

> With appologies to all the really good programmers, could someone show me

> an example of "prepend.inc"? It is used on pg 541 of Por PHP. What I am

> trying to do is use this so that I can put my header and footer in one

> place rather than make changes to each individual page. Any help/advice

> appreciated.

> -Allan




> $subst('Email.Unsub').



Message #3 by "Lawrence" <lkrubner@g...> on Sat, 23 Feb 2002 17:31:41 -0500
From: "Nikolai Devereaux" <yomama@u...>

> Does that make sense?  I prefer require_once or include_once over their

> non-_once versions unless I specifically WANT the file to be inserted and

> parsed multiple times.  It's much more efficient to use the _once versions

> (especially if your include files are long or just function definitions)

> because the interpreter doesn't have to parse through them unnecessarily.



I'm not sure I really understand include_once() . I've written an application with 3 files, a

control panel file, a functions file, and a config file. The config file holds passwords, the

functions file holds all the functions, and the control panel is what the user actually interacts

with. The other two files are included() on the control panel. Many different forms are printed to

the screen, and the action of all of them points back to the control file, so the user is never

interacting with anything except that one page (though what is printed to screen changes constantly,

of course). I suppose this is a candidate for include_once() ? I'm wondering how much of a

performance penalty I take by using include() ?



A few interesting notes and warnings about include_once from www.php.net :





include_once() should be used in cases where the same file might be included and evaluated more than

once during a particular execution of a script, and you want to be sure that it is included exactly

once to avoid problems with function redefinitions, variable value reassignments, etc.



----------------------------------------





If you call twice include_once() using the same include file, the second

include_once() will return false.



So, you have to use get_included_files() to check if the file was already

included or if the file does not exist.



$res = include_once('path/file.inc');

if(!res) {

  if( !in_array('path/file.inc', get_included_files()) ) {

    echo "The file 'path/file.inc' does not exists";

    exit(1);

  }

}



------------



In PHP 4.0.6, you have put a 'return;' statement in your include file,

something like this:



<?php

// this is a file to be readed by inlude_once()

return;

?>



If you don't put the 'return;' statement in the include file, and then

call session_start() after include_once(), you will get and error.



------------------------------------





Interesting note, and something that was rather funny at the time.  Doing:



include_once "";



will not result in an error, but will result in preventing the page from

not being displayed.  Why this is, I have my suspicions, but not the

desire to dig deeper into PHP's C code to find out.  However, if you are

running into a problem where you know the page is there, and you can't see

it, but you have the permissions, double check for something like this.

Quite funny indeed. =)












  Return to Index