473,406 Members | 2,371 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,406 software developers and data experts.

Unset won't work?

Hi group,

Why won't $new_var be unset in the following function?
Am i missing out something?

Greetings Frizzle.

*************************************
function DoThis( $state, $fst = 1, $scnd = 2)
{
if( $state === 0 )
{
global $new_var;
unset($new_var);
}
else
{
global $new_var;
return $new_var = $fst + $scnd;
};
};
echo DoThis( 1, 4, 8);
echo DoThis( 0 );
echo 'new_var= '.$new_var;

Oct 4 '05 #1
1 1860
frizzle wrote:
Hi group,

Why won't $new_var be unset in the following function?
Am i missing out something?

Greetings Frizzle.

*************************************
function DoThis( $state, $fst = 1, $scnd = 2)
{
if( $state === 0 )
{
global $new_var;
unset($new_var);
- global $new_var;
- unset($new_var);
+ unset($GLOBAL['new_var'];

See the docs for details (www.php.net/unset)
}
else
{
global $new_var;
return $new_var = $fst + $scnd;
};
};
echo DoThis( 1, 4, 8);
echo DoThis( 0 );
echo 'new_var= '.$new_var;


Note: you don't need ; after the curly braces. And you should
probably also initialize the $new_var before DoThis();

Anyway, IMHO, unsetting global variables within functions is a sure
way of shooting yourself in the foot. BTW, because you actually unset
the variable the last line will throw a notice about undef variable.

/m
Oct 4 '05 #2

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

Similar topics

7
by: lawrence | last post by:
Given a bunch of mixes variables, I'm in a situation where I can't know what type they are. Can I use unset to kill an object? unset($object); Can I use unset to kill a pointer to an open...
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...
5
by: ahevans | last post by:
Howdy all, I'm progressing with PHP but I have 2 questions that are kinda bugging me. Say for example, I have a php page similar to this.... if ..... if........
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...
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...
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>...
1
by: Nico | last post by:
Hi all, I've created the following code: <?php session_start(); ?> <FORM METHOD="POST" ACTION="prova.php"> Add <b>Combination</b><br><br>
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.