Since your original question has already been given, let me suggest a different approach that avoids global variables. It seems you have a function + default value that you set as a global variable. You can also define default values ββfor function parameters .
<?php // test.php function test($flags=4) { echo '<br/>in test flag="'.$flags.'"'; if ($flags!=0) { echo ' flag works'; } }
There is no global variable, but you can still call test () without a parameter, and the function will use the default value, which you can overwrite by passing the parameter.
function something() { $include_file = 'test.php'; if ( file_exists($include_file) ) { require_once ($include_file); echo "\nwithout parameter: "; test(); echo "\nwith a parameter: "; test(16); } } something(); exit;
prints
without parameter: <br/>in test flag="4" flag works with a parameter: <br/>in test flag="16" flag works
This is not exactly the same behavior, but it may be what you wanted in the first place ....
edit: Another point could be
if ( file_exists($include_file) ) { require_once ($include_file);
which you use. I do not have a strong opinion (or any convincing evidence ;-)), but you can consider using
$something = include $include_file; if ( false!==$something ) { }
instead of this. include "returns" false if php cannot include the file. All that is required for this is that the included file does not explicitly return FALSE. This may be more cache friendly (for example, when using something like APC ). But then again: I have no evidence of this, just a hunch. At the very least, it gives your code the ability to handle the error gracefully.
Volkerk
source share