473,241 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Donít unset that variable you need globally

Dontí make my mistake. It is costly.

Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO: $var = null;
DONT DO: unset($var);

Why? Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....

--
Posted using the http://www.dbforumz.com interface, at author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbforumz.com/PHP-Don-unse...ict237516.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=825331
Jul 17 '05 #1
21 3337
steve wrote:
Dontí make my mistake. It is costly.

Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO: $var = null;
DONT DO: unset($var);

Why? Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....

Thanks for the heads up, although I do remember reading something along
these lines in the PHP manual.
Jul 17 '05 #2
steve wrote:
Dontí make my mistake. It is costly.

Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO: $var = null;
DONT DO: unset($var);

Why? Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....


Even better -- don't use global variables.

Berislav
Jul 17 '05 #3
"" wrote:
steve wrote:
Dontí make my mistake. It is costly.

Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO: $var = null;
DONT DO: unset($var);

Why? Because the 2nd way removes the variable from the name

space,
and it is no longer globally available. Cost me 10 hours....

Thanks for the heads up, although I do remember reading
something along
these lines in the PHP manual.


You are welcome, and if you find that inf on php site, please post.

--
Posted using the http://www.dbforumz.com interface, at author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbforumz.com/PHP-Don-unse...ict237516.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=825739
Jul 17 '05 #4
"steve" <Us************@dbForumz.com> wrote in message
news:4_***************************************@dbf orumz.com...
Dontí make my mistake. It is costly.

Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO: $var = null;
DONT DO: unset($var);

Why? Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....


That's odd. The manual seems to disagree:

"If a globalized variable is unset() inside of a function, only the local
variable is destroyed. The variable in the calling environment will retain
the same value as before unset() was called.

<?php
function destroy_foo()
{
global $foo;
unset($foo);
}

$foo = 'bar';
destroy_foo();
echo $foo; // prints "bar"
?>"
- from http://fi.php.net/manual/en/function.unset.php

Now I don't know what to believe..

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com
Jul 17 '05 #5
> Steve wrote:
Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO: $var = null;
DONT DO: unset($var);

Why? Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....

Kimmo Laine wrote: That's odd. The manual seems to disagree:

"If a globalized variable is unset() inside of a function, only the local
variable is destroyed. The variable in the calling environment will retain
the same value as before unset() was called.

<?php
function destroy_foo()
{
global $foo;
unset($foo);
}

$foo = 'bar';
destroy_foo();
echo $foo; // prints "bar"
?>"
- from http://fi.php.net/manual/en/function.unset.php


That's exactly what Steve said. He said that the variable is removed
from current namespace (function namespace), not that it's totaly
removed.

"global" keyword is like setting local reference to global variable.
When you call "unset", then you unset reference to that variable.
To "unset" global variable from a function you'll have to
"unset( $_GLOBAL['foo'] )".
Hilarion
Jul 17 '05 #6
> > steve wrote:
Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO: $var = null;
DONT DO: unset($var);

Why? Because the 2nd way removes the variable from the name
space,
and it is no longer globally available. Cost me 10 hours....
[...] if you find that inf on php site, please post.


Read Kimmo Laine post. It contains the link and manual fragment
which describes the case ("the local variable is destroyed").

Hilarion
Jul 17 '05 #7
"Hilarion" <hi******@SPAM.op.SMIECI.pl> wrote in message
news:da**********@news.onet.pl...
Steve wrote:
> Say you have defined a variable $var in your main script.
>
> Now in a function you access it using:
> global $var;
>
> But you want to set it to null inside the function.
> DO: $var = null;
> DONT DO: unset($var);
>
> Why? Because the 2nd way removes the variable from the name space,
> and it is no longer globally available. Cost me 10 hours....


Kimmo Laine wrote:
That's odd. The manual seems to disagree:

"If a globalized variable is unset() inside of a function, only the local
variable is destroyed. The variable in the calling environment will
retain the same value as before unset() was called.

<?php
function destroy_foo()
{
global $foo;
unset($foo);
}

$foo = 'bar';
destroy_foo();
echo $foo; // prints "bar"
?>"
- from http://fi.php.net/manual/en/function.unset.php


That's exactly what Steve said. He said that the variable is removed
from current namespace (function namespace), not that it's totaly
removed.

I somehow thought he meant from the entire namespace, not from current...
Okay, my bad. Now it all makes sense.

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com
Jul 17 '05 #8

"Kimmo Laine" <et******************@Mgmail.com> wrote in message
news:DV*****************@reader1.news.jippii.net.. .
| "Hilarion" <hi******@SPAM.op.SMIECI.pl> wrote in message
| news:da**********@news.onet.pl...
| >> Steve wrote:
| >> > Say you have defined a variable $var in your main script.
| >> >
| >> > Now in a function you access it using:
| >> > global $var;
| >> >
| >> > But you want to set it to null inside the function.
| >> > DO: $var = null;
| >> > DONT DO: unset($var);
| >> >
| >> > Why? Because the 2nd way removes the variable from the name space,
| >> > and it is no longer globally available. Cost me 10 hours....
| >
| > Kimmo Laine wrote:
| >> That's odd. The manual seems to disagree:
| >>
| >> "If a globalized variable is unset() inside of a function, only the
local
| >> variable is destroyed. The variable in the calling environment will
| >> retain the same value as before unset() was called.
| >>
| >> <?php
| >> function destroy_foo()
| >> {
| >> global $foo;
| >> unset($foo);
| >> }
| >>
| >> $foo = 'bar';
| >> destroy_foo();
| >> echo $foo; // prints "bar"
| >> ?>"
| >> - from http://fi.php.net/manual/en/function.unset.php
| >
| > That's exactly what Steve said. He said that the variable is removed
| > from current namespace (function namespace), not that it's totaly
| > removed.
|
|
| I somehow thought he meant from the entire namespace, not from current...
| Okay, my bad. Now it all makes sense.

hmmm...i don't believe you have mis-read anything. i read steve's op that
same way you did *and* the problem he describes *does* conflict with the php
manual. but then again, we may both be having problems with our reading
comprehension. ;^)
Jul 17 '05 #9
> hmmm...i don't believe you have mis-read anything. i read steve's op that
same way you did *and* the problem he describes *does* conflict with the php
manual. but then again, we may both be having problems with our reading
comprehension. ;^)

:)

OK. The way I read it was:
Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;
I suppose the above text was clear and requires no translation.

But you want to set it to null inside the function.
The above means "you want to unset global variable $var / set it
to null"

DO: $var = null;
DONT DO: unset($var);

Why? Because the 2nd way removes the variable from the name space,
"Because the 2nd way removes the variable from current (function) namespace"

and it is no longer globally available.
"You do not have acces to the global variable from current (function) scope."

Cost me 10 hours....


Hilarion

PS.: The fact that English is not my native language is probably influencing
the way that I read it.
Jul 17 '05 #10
Berislav Lopac wrote:
steve wrote:
Dont' make my mistake. It is costly.

Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO: $var = null;
DONT DO: unset($var);

Why? Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....


Even better -- don't use global variables.


Easier said than done. There are times when globals are the best solution.
There IS a reason they're available...

--
Tony Garcia
Web Right! Development
Jul 17 '05 #11
On 5 Jul 2005 15:38:12 -0400, steve <Us************@dbForumz.com> wrote:
That was my point, sorry for causing confusion. Thanks for reference.
So unset does not work inside a function for global vars- it only
destroys local var.

The workaround is to say
$var = null;
inside functions.


Although the variable remains set that way - it's just null.

To get rid of it completely: unset($GLOBALS['var']);

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #12
In comp.lang.php steve <Us************@dbforumz.com> wrote:
This is to my counter-intuitive, and unless you read the manual, one
would not know.


$DEITY forbid that anyone should read the manual!

But one could consider the global keyword to be extremely evil (changing
scopes of variables is extremly yuck), use the superglobal $GLOBALS
instead (note the unintuitive missing '_').

FUP to comp.lang.php
Jul 17 '05 #13
> "unset( $_GLOBAL['foo'] )".

Should be:

"unset( $GLOBALS['foo'] )"

My mistake (I noticed it after reading post of Daniel Tryba).
Hilarion
Jul 17 '05 #14
In comp.lang.php Hilarion <m0rt_nospam_@nospam_bez_spamu_itepe_poczta.onet.p l> wrote:
"unset( $_GLOBAL['foo'] )".

Should be:

"unset( $GLOBALS['foo'] )"

My mistake (I noticed it after reading post of Daniel Tryba).


Hehehe :)

A good thing I missed your posting and essentially wrote the same as in
a double blindfolded second opinion test :)

Jul 17 '05 #15
"" wrote:
In comp.lang.php Hilarion
<m0rt_nospam_@nospam_bez_spamu_itepe_poczta.onet.p l> wrote:
"unset( $_GLOBAL['foo'] )".

Should be:

"unset( $GLOBALS['foo'] )"

My mistake (I noticed it after reading post of Daniel

Tryba).

Hehehe :)

A good thing I missed your posting and essentially wrote the
same as in
a double blindfolded second opinion test :)


ok, so what is the difference between:

unset($var)
and
$var = null;

I know the first form removes the $var from name space, but is there
any functions to check the difference. isset() responds the same way
to both.

--
Posted using the http://www.dbforumz.com interface, at author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbforumz.com/PHP-Don-unse...ict237516.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=826716
Jul 17 '05 #16
In comp.lang.php steve <Us************@dbforumz.com> wrote:
ok, so what is the difference between:

unset($var)
and
$var = null;

I know the first form removes the $var from name space, but is there
any functions to check the difference. isset() responds the same way
to both.


http://php.net/isset says it all:

"isset() will return FALSE if testing a variable that has been set to
NULL."

I have no idea why (and who decided that) a variable set to null is
considered unset (and a reference to a variable with value null not
raising a "Undefined variable" notice). IMHO it simply should return
true since it is just plain inconsistent.

Jul 17 '05 #17
"" wrote:
In comp.lang.php steve <Us************@dbforumz.com> wrote:
ok, so what is the difference between:

unset($var)
and
$var = null;

I know the first form removes the $var from name space, but

is there
any functions to check the difference. isset() responds the

same way
to both.


http://php.net/isset says it all:

"isset() will return FALSE if testing a variable that has been
set to
NULL."

I have no idea why (and who decided that) a variable set to
null is
considered unset (and a reference to a variable with value
null not
raising a "Undefined variable" notice). IMHO it simply should
return
true since it is just plain inconsistent.


I am looking for something like defined(CONSTANT) but I guess not..

--
Posted using the http://www.dbforumz.com interface, at author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbforumz.com/PHP-Don-unse...ict237516.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=826852
Jul 17 '05 #18
Tony wrote:
Berislav Lopac wrote:
Even better -- don't use global variables.


Easier said than done. There are times when globals are the best
solution. There IS a reason they're available...


You can (and should) always pass a global variable as a function parameter.
There is no effective difference between:

function foo($bar)
{
global $foobar;
return $bar + $foobar;
}

and

function foo($bar, &$foobar)
{
return $bar + $foobar;
}

The difference is that when you call the second example you have to pass one
more argument, but that prevents the encapsulation of the function, making
it reusable and independent of the outside world.

Berislav
Jul 17 '05 #19
| I am looking for something like defined(CONSTANT) but I guess not..

what?

you're crossing my wires. are you trying to define a constant or work with
variable scope? why not just post your code that's giving you fits. i'd be
willing to bet that the problem is not with either constants or globals.
but, that's just me.
Jul 17 '05 #20
In comp.lang.php steve <a@b.com> wrote:
| I am looking for something like defined(CONSTANT) but I guess not..

what?

you're crossing my wires. are you trying to define a constant or work with
variable scope? why not just post your code that's giving you fits. i'd be
willing to bet that the problem is not with either constants or globals.
but, that's just me.


No no, Steve was asking an existentialistic question:

How to check if a variable is set?

isset() is not the answer to the question, easily demonstrated with a
little script which outputs:
$ php4 ./huh.php
isset(foo): false, isset(bar): false

Notice: Undefined variable: bar in /tmp/huh.php on line 8
foo: [], bar: []

Both $foo and $bar aren't set (according ot isset()), using them anyway
results in an undefined notice for $bar only. So $foo is defined. But
defined() is for contants only.

So the question remains how to findout if a variable is set. A
workaround for variables in the global scope exists by checking for the
key in the $GLOBALS array:
keyexists(foo): true, keyexists(bar): false

But how can this check be computed for variables in any other scope?

My sample code:
<?php
error_reporting(E_ALL);

$foo=null;

echo "isset(foo): ".(isset($foo)?"true":"false").", isset(bar): ".(isset($bar)?"true":"false")."\n";

echo "foo: [$foo], bar: [$bar]\n";

echo "keyexists(foo): ".(array_key_exists('foo',$GLOBALS)?"true":"false" ).", keyexists(bar): ".(array_key_exists('bar',$GLOBALS)?"true":"false" )."\n";
?>

Jul 17 '05 #21
gotcha.
"Daniel Tryba" <pa**********@invalid.tryba.nl> wrote in message
news:42***********************@news6.xs4all.nl...
| In comp.lang.php steve <a@b.com> wrote:
| > | I am looking for something like defined(CONSTANT) but I guess not..
| >
| > what?
| >
| > you're crossing my wires. are you trying to define a constant or work
with
| > variable scope? why not just post your code that's giving you fits. i'd
be
| > willing to bet that the problem is not with either constants or globals.
| > but, that's just me.
|
| No no, Steve was asking an existentialistic question:
|
| How to check if a variable is set?
|
| isset() is not the answer to the question, easily demonstrated with a
| little script which outputs:
| $ php4 ./huh.php
| isset(foo): false, isset(bar): false
|
| Notice: Undefined variable: bar in /tmp/huh.php on line 8
| foo: [], bar: []
|
| Both $foo and $bar aren't set (according ot isset()), using them anyway
| results in an undefined notice for $bar only. So $foo is defined. But
| defined() is for contants only.
|
| So the question remains how to findout if a variable is set. A
| workaround for variables in the global scope exists by checking for the
| key in the $GLOBALS array:
| keyexists(foo): true, keyexists(bar): false
|
| But how can this check be computed for variables in any other scope?
|
| My sample code:
| <?php
| error_reporting(E_ALL);
|
| $foo=null;
|
| echo "isset(foo): ".(isset($foo)?"true":"false").", isset(bar):
".(isset($bar)?"true":"false")."\n";
|
| echo "foo: [$foo], bar: [$bar]\n";
|
| echo "keyexists(foo):
".(array_key_exists('foo',$GLOBALS)?"true":"false" ).", keyexists(bar):
".(array_key_exists('bar',$GLOBALS)?"true":"false" )."\n";
| ?>
|
Jul 17 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
PHP: unset($_SESSION); In my original environment (PHP 4.3.2) this line will delete the session variable 'mainDisplay'. But in the testing environment (PHP 4.3.6) the variable persists even...
2
by: Steve | last post by:
I'm working on an e-commerce site, and one of the things I need to do is split an existing order into two orders. The problem I'm having is not creating the new order, but getting the remaining...
3
by: fasanay | last post by:
Hi everybody I have got the following PHP code which I am trying to convert to ASP any help will be appreciated...I have done most of it but I cant find a replace function for Unset in asp which...
1
by: Rob Wire | last post by:
Please let me know the preferred way to do the following. Accept a Variable in the calling page's URL of an ID. Set that variable to be global scope of the class so that it can be used throughout...
7
by: fasanay | last post by:
Hi everybody I have got the following PHP code which I am trying to convert to ASP any help will be appreciated...I have done most of it but I cant find a replace function for Unset in asp which...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
2
by: Georg Weiler | last post by:
Hi, I'm biting my nails on this for several days now, hope that someone of you can help me...: On my page, the user can display tables, created out of a database. So I have several <a href>...
7
by: lievendp | last post by:
Just wondering, Is it possible for an instance of a class to unset itself? can I do "unset($this)" or "unset(&$this)" regards, Lieven http://eye.cc php newsgroups
4
by: mtuller | last post by:
I have a page that submits data to a database, and I want to make it so that if the page is refreshed, it doesn't submit the information again. I am trying to use unset the variables so that if the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.