Re: PHP4.0.1pl2 Bug? unset() on assoc array inside function

From: Date: Thu, 13 Jul 2000 01:35:23 +0000
Subject: Re: PHP4.0.1pl2 Bug? unset() on assoc array inside function
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-24384@lists.php.net to get a copy of this message
> Hello All, > > I am in the process of porting SourceForge to PHP4. Congratulations on > the overall level of compatibility. Frankly, we were stunned that the SF > code ran under PHP4 on first try. > > There are a couple of tiny issues we're running into as I comb through > the security of the code under PHP4. I apologize if this is already > reported or handled. If anyone feels this is a real bug, I'll go ahead > and submit it through your tracker. > > Anyway: calling unset() on a global associative array inside a function > will cause that array to not be global anymore. This is not consistent > behavior with calling unset() on a variable inside a function. > > Here's an example of broken code: > > > $G_USER=array(); > > function session_setglobals($user_id) { > global $G_USER; > > unset($G_USER); > file://if I call unset() here - the array > file://is not global anymore and cannot be > file://seen outside this function > > if ($user_id > 0) { > $result=db_query("SELECT user_id,user_name > FROM user WHERE user_id='$user_id'"); > if (!$result || db_numrows($result) < 1) { > file://echo db_error(); > $G_USER = array(); > } else { > $G_USER = db_fetch_array($result); > // echo $G_USER['user_name'].'<BR>'; > } > } else { > $G_USER = array(); > } > } > > > > This code behaves properly (or as I expect it to): > > > > > unset($USER_IS_SUPER_USER); > > function user_is_super_user() { > global $USER_IS_SUPER_USER; > /* > members of group_id 1 who are admins have super-user > privs site-wide > */ > > if (isset($USER_IS_SUPER_USER)) { > return $USER_IS_SUPER_USER; > } else { > $sql="SELECT * FROM user_group WHERE user_id='". > user_getid() ."' AND group_id='1' AND admin_ > $result=db_query($sql); > if (!$result || db_numrows($result) < 1) { > $USER_IS_SUPER_USER=false; > return $USER_IS_SUPER_USER; > } else { > file://matching row was found - set and save this > knowledge for later > $USER_IS_SUPER_USER=true; > return $USER_IS_SUPER_USER; > } > } > } > This is due to unset being a statement, not a function. Documented incompatibility. -Sterling

« previous php.dev (#24384) next »