|
Posted by ZeldorBlat on 11/01/05 02:47
>I've had a good look at previous posts concerning this warning, and see >that isset() is recommended to prevent this kind of warning. Is this totally >necessary?
isset() prevents the warning, but there's a bigger issue here. Not so
much caring whether the variable was set, but where it might have been
set. Suppose you include file inc1.php and he sets some variable.
Then later you include inc2.php. You can't really be sure that by the
time you get to inc2 the value of the variable is the same as it was
when you left inc1. Perhaps it shouldn't be if some other processing
occured, but this point leads into the next question:
>In ASP, and include file becomes part of the total script for the page
>that calls the include. Is this different in PHP? Is each include
>treated as a separate script?
When you use include() or require(), you've effectively picked up any
code in that file and dropped it in where you call include(). That
means that the code in the included file lives in the same scope where
include() was called. For instance, suppose you had an include file
called inc1.php that contained the following:
<?
echo $foo;
?>
In your main script, you have something like this:
$foo = 5;
function bar($x) {
$foo = $x+1; //this foo is local to the function bar
include('inc1.php');
return $foo;
}
bar(1); //call the function
This will output 2. However, if you move the include() outside the
function declaration the script will output 5.
Navigation:
[Reply to this message]
|