Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP Variables unloading

LEGEND ,
Aug 24, 2006 Aug 24, 2006
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?

--

TIA,

Jon Parkhurst
PriivaWeb
http://priiva.net.


TOPICS
Server side applications
724
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 24, 2006 Aug 24, 2006
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 24, 2006 Aug 24, 2006
> 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.

That supports my testing. Currently, I have my connection file included in
the main body of the document, my 'most common' recordsets included in
another file, and gerneralFunctions included in a third. These three
operate seamlessly together.



> 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.

Agreed, again my testing supports this as well and sit is working on this
principle.

> the only way I know would be to do something like
>
> $myvar = '';

That's what I was afraid of. It might be so, I just wish I could use the
isset to check and see ifthey're still valid or not. I'm betting that if
$myVar = '', it will still be T for isset.


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 24, 2006 Aug 24, 2006
On Thu 24 Aug 2006 05:44:52p, crash wrote in
macromedia.dreamweaver.appdev:

> That's what I was afraid of. It might be so, I just wish I could use
> the isset to check and see ifthey're still valid or not. I'm betting
> that if $myVar = '', it will still be T for isset.

For some reason, and maybe it's the servers/version of PHP I'm on, I've
never gotten good results out of isset(). I try it, but invariably wind up
back at strlen($whatever) == 0 ?.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 24, 2006 Aug 24, 2006
On Thu 24 Aug 2006 05:44:52p, crash wrote in
macromedia.dreamweaver.appdev:

>> the only way I know would be to do something like
>>
>> $myvar = '';
>
> That's what I was afraid of. It might be so, I just wish I could use
> the isset to check and see ifthey're still valid or not. I'm betting
> that if $myVar = '', it will still be T for isset.

There is, however, this:

http://www.php.net/manual/en/function.unset.php

Haven't tried it myself. !@#$, that search function at php.net works
wonders...
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 24, 2006 Aug 24, 2006
Joe Makowiec wrote:
>>That's what I was afraid of. It might be so, I just wish I could use
>>the isset to check and see ifthey're still valid or not. I'm betting
>>that if $myVar = '', it will still be T for isset.
>
> For some reason, and maybe it's the servers/version of PHP I'm on, I've
> never gotten good results out of isset().

No, you've got good results out of isset(). A variable that has been
defined returns true with isset(). To test for a variable that has no
value, use empty().

As you have just discovered in your other post, unset() removes a
variable entirely.

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 25, 2006 Aug 25, 2006
yeah yeah yeah! Thanks Joe! I think that's exactly what I'm lookin gfor.

All these search engines work great - if you know wha tto search for. I
invariably choose the worst wording.

Thanks agina mate!

Jon
"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns9829B80111472makowiecatnycapdotrE@216.104.212.96...
> On Thu 24 Aug 2006 05:44:52p, crash wrote in
> macromedia.dreamweaver.appdev:
>
>>> the only way I know would be to do something like
>>>
>>> $myvar = '';
>>
>> That's what I was afraid of. It might be so, I just wish I could use
>> the isset to check and see ifthey're still valid or not. I'm betting
>> that if $myVar = '', it will still be T for isset.
>
> There is, however, this:
>
> http://www.php.net/manual/en/function.unset.php
>
> Haven't tried it myself. !@#$, that search function at php.net works
> wonders...


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 25, 2006 Aug 25, 2006
Thanks David for the help!


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 25, 2006 Aug 25, 2006
yeah yeah yeah! Thanks Joe! I think that's exactly what I'm lookin gfor.

All these search engines work great - if you know wha tto search for. I
invariably choose the worst wording.

Thanks agina mate!

Jon
"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns9829B80111472makowiecatnycapdotrE@216.104.212.96...
> On Thu 24 Aug 2006 05:44:52p, crash wrote in
> macromedia.dreamweaver.appdev:
>
>>> the only way I know would be to do something like
>>>
>>> $myvar = '';
>>
>> That's what I was afraid of. It might be so, I just wish I could use
>> the isset to check and see ifthey're still valid or not. I'm betting
>> that if $myVar = '', it will still be T for isset.
>
> There is, however, this:
>
> http://www.php.net/manual/en/function.unset.php
>
> Haven't tried it myself. !@#$, that search function at php.net works
> wonders...


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 25, 2006 Aug 25, 2006
Thanks David for the help!


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 25, 2006 Aug 25, 2006
yeah yeah yeah! Thanks Joe! I think that's exactly what I'm lookin gfor.

All these search engines work great - if you know wha tto search for. I
invariably choose the worst wording.

Thanks agina mate!

Jon
"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns9829B80111472makowiecatnycapdotrE@216.104.212.96...
> On Thu 24 Aug 2006 05:44:52p, crash wrote in
> macromedia.dreamweaver.appdev:
>
>>> the only way I know would be to do something like
>>>
>>> $myvar = '';
>>
>> That's what I was afraid of. It might be so, I just wish I could use
>> the isset to check and see ifthey're still valid or not. I'm betting
>> that if $myVar = '', it will still be T for isset.
>
> There is, however, this:
>
> http://www.php.net/manual/en/function.unset.php
>
> Haven't tried it myself. !@#$, that search function at php.net works
> wonders...


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 25, 2006 Aug 25, 2006
Thanks David for the help!


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 25, 2006 Aug 25, 2006
yeah yeah yeah! Thanks Joe! I think that's exactly what I'm lookin gfor.

All these search engines work great - if you know wha tto search for. I
invariably choose the worst wording.

Thanks agina mate!

Jon
"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns9829B80111472makowiecatnycapdotrE@216.104.212.96...
> On Thu 24 Aug 2006 05:44:52p, crash wrote in
> macromedia.dreamweaver.appdev:
>
>>> the only way I know would be to do something like
>>>
>>> $myvar = '';
>>
>> That's what I was afraid of. It might be so, I just wish I could use
>> the isset to check and see ifthey're still valid or not. I'm betting
>> that if $myVar = '', it will still be T for isset.
>
> There is, however, this:
>
> http://www.php.net/manual/en/function.unset.php
>
> Haven't tried it myself. !@#$, that search function at php.net works
> wonders...


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 25, 2006 Aug 25, 2006
yeah yeah yeah! Thanks Joe! I think that's exactly what I'm lookin gfor.

All these search engines work great - if you know wha tto search for. I
invariably choose the worst wording.

Thanks agina mate!

Jon
"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns9829B80111472makowiecatnycapdotrE@216.104.212.96...
> On Thu 24 Aug 2006 05:44:52p, crash wrote in
> macromedia.dreamweaver.appdev:
>
>>> the only way I know would be to do something like
>>>
>>> $myvar = '';
>>
>> That's what I was afraid of. It might be so, I just wish I could use
>> the isset to check and see ifthey're still valid or not. I'm betting
>> that if $myVar = '', it will still be T for isset.
>
> There is, however, this:
>
> http://www.php.net/manual/en/function.unset.php
>
> Haven't tried it myself. !@#$, that search function at php.net works
> wonders...


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 25, 2006 Aug 25, 2006
yeah yeah yeah! Thanks Joe! I think that's exactly what I'm lookin gfor.

All these search engines work great - if you know wha tto search for. I
invariably choose the worst wording.

Thanks agina mate!

Jon
"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns9829B80111472makowiecatnycapdotrE@216.104.212.96...
> On Thu 24 Aug 2006 05:44:52p, crash wrote in
> macromedia.dreamweaver.appdev:
>
>>> the only way I know would be to do something like
>>>
>>> $myvar = '';
>>
>> That's what I was afraid of. It might be so, I just wish I could use
>> the isset to check and see ifthey're still valid or not. I'm betting
>> that if $myVar = '', it will still be T for isset.
>
> There is, however, this:
>
> http://www.php.net/manual/en/function.unset.php
>
> Haven't tried it myself. !@#$, that search function at php.net works
> wonders...


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 25, 2006 Aug 25, 2006
Thanks David for the help!


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 05, 2006 Oct 05, 2006
David-

So, this:

if(!empty($var)){

is the same as this:

if($var=""){

correct?

Thanks,

Jon


"David Powers" <david@example.com> wrote in message
news:eclcuo$hbd$2@forums.macromedia.com...
> Joe Makowiec wrote:
>>>That's what I was afraid of. It might be so, I just wish I could use
>>>the isset to check and see ifthey're still valid or not. I'm betting
>>>that if $myVar = '', it will still be T for isset.
>>
>> For some reason, and maybe it's the servers/version of PHP I'm on, I've
>> never gotten good results out of isset().
>
> No, you've got good results out of isset(). A variable that has been
> defined returns true with isset(). To test for a variable that has no
> value, use empty().
>
> As you have just discovered in your other post, unset() removes a variable
> entirely.
>
> --
> David Powers
> Adobe Community Expert
> Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
> http://foundationphp.com/


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 05, 2006 Oct 05, 2006
On Thu, 5 Oct 2006 12:01:18 -0500, "crash" <crash@bcdcdigital.com>
wrote:

>So, this:
>
>if(!empty($var)){
>
>is the same as this:
>
>if($var=""){
>
>correct?

No. You mean:

if($var != "") {

--
Steve
steve at flyingtigerwebdesign dot com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 05, 2006 Oct 05, 2006
LATEST
😉 thanks for the catch mate. and I just tested it within the code I was
using and it works great!

Jon


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines