On Thu 24 Aug 2006 04:36:46p, crash wrote in
macromedia.dreamweaver.appdev:
> I have a lot of included files in my site. At the end of
each included
> file, I close the sql, but would also like to close the
variables used
> to produce the HTML.
>
> I'm having a hard time with my searching getting some
relevant
> results. Can somebody help me here on what I"m looking
for?
I'm not sure I follow you here. Are you saying that you want
to limit
the scope of the PHP variables to the included file, so that
in something
like this:
<?php
include_once('fileA.inc');
include_once('fileB.inc');
include_once('fileC.inc');
?>
you can have a variable, say $crashsVariable, in fileA.inc,
fileB.inc and
fileC.inc, and have it take on a separate value in each of
them, but not
affect any of the others?
Without doing any experimentation... Using the
include_once()s makes the
include files, and thus all their code, part of the larger
code block.
AFAIK, the scope of a variable would be the calling file, so
the answer
would be no.
The PHP docs[1] have this to say:
The scope of a variable is the context within which it is
defined.
For the most part all PHP variables only have a single
scope. This
single scope spans included and required files as well.
but go on to add:
However, within user-defined functions a local function
scope is
introduced. Any variable used inside a function is by
default limited
to the local function scope.
So if you use $crashsVariable within a function within the
included file
(or, for that matter, the main file), it's local in scope.
Otherwise, a
variable exists for the entire page.
If you've spent the include file building a long string of
text, which is
eventually output as HTML:
<?php
$myvar = "<head>\n";
$myvar .= "<title>$myDynamicTitle</title>\n";
...
echo $myvar;
?>
the only way I know would be to do something like
$myvar = '';
Hopefully David Powers or somebody who actually knows might
check in.
[1]
http://www.php.net/variables.scope