note 87556 modified in function.include by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 00:51:26 +0000 |
| Subject: | note 87556 modified in function.include by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155441@lists.php.net to get a copy of this message | ||
If you wish to abstract away include calls inside functions, or programmatically juggle files to
include using functions, just remember:
1. Declare any variables as global if you want those variables "included" in the global
scope (ie. if they are used outside the file).
2. Functions are naturally global, so files that only contain functions (libs, sets of api's
what have you) can be included anywhere.
eg.
<?php
function nav($i){
include "nav$i.php";
}
nav(1);
// same as...
include "nav1.php";
// ...as long as variables are global
?>
So don't feel you can only include/require at the beginning of files, or outside/before
functions. You can totally program any sophisticated include behavior.
--was--
If you wish to abstract away include calls inside functions, or programmatically juggle files to
include using functions, just remember:
1. Declare any variables as global if you want those variables "included" in the global
scope (ie. if they are used outside the file).
2. Functions are naturally global, so files that only contain functions (libs, sets of api's
what have you) can be included anywhere.
eg.
function nav($i){
include "nav$i.php";
}
nav(1);
// same as...
include "nav1.php";
// ...as long as variables are global
So don't feel you can only include/require at the beginning of files, or outside/before
functions. You can totally program any sophisticated include behavior.
http://php.net/manual/en/function.include.php