473,624 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3378
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_******** *************** *************** *@dbforumz.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.c om> wrote in message
news:DV******** *********@reade r1.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

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

Similar topics

3
14900
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 after unset() is called. Is there some special setting in PHP that can allow a session to
2
3129
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 items from the original order cleaned up in the array. What I've tried to do so far is: 1) The data is stored in a serialized array in the order_data field in the orders table. When the order is selected, it is unserialized and called $order_data....
3
4105
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 will discard the variable alltogether... if ($categoryid == "all") { $sql = "SELECT * FROM products where shopinspection=$shopinspection"; unset($HTTP_POST_VARS); unset($HTTP_POST_VARS);
1
3676
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 the functions from Page Load. Would like to send the ID variable as a SQL parameter to stored procedures for Select, Update, and Insert. What I have done so far to get around it is declare it in the SQL Select statements of the Web form code,...
7
1850
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 will discard the variable alltogether... if ($categoryid == "all") { $sql = "SELECT * FROM products where shopinspection=$shopinspection"; unset($HTTP_POST_VARS); unset($HTTP_POST_VARS);
10
2246
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 multiplyer as integer, ByVal variable as ___?) variable = 5 * multiplyer end function What I would like this function to do is take the name of the incoming variable and assign a calculated value to it. Any help would be greatly appreciated, TIA!!
2
8955
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> links to display the various tables. They all look something like: <a href=this_page.php?table=mytable1>the first table</a>
7
5232
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
45422
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 page is refreshed, it will not submit again. I saw a post about setting a time delay of something like 30 seconds, but would prefer just to unset the variable. I can't seem to get it working. Here is a snippet of what I have: echo 'First...
0
8240
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.