php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #1975 A recursive function is not acting correctly in 4.0b2 that works fine in 3.0
Submitted: 1999-08-05 13:30 UTC Modified: 1999-08-06 13:52 UTC
From: mstearne at eisolutions dot com Assigned:
Status: Closed Package: Other
PHP Version: 4.0 Latest CVS (05/08/1999) OS: Linux 2.2.5 (RH 6)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mstearne at eisolutions dot com
New email:
PHP Version: OS:

 

 [1999-08-05 13:30 UTC] mstearne at eisolutions dot com
This function should (and does in 3.0) output 
* Computers 
     * Desktops
     * Notebooks
     * Hand Held
* Video Games 

for and number of levels of Cat/Sub-Categories.  
In 4.0 the output is 

* Computers 
* Video Games 

This also affects other functions that were built out of code like this.

<pre>
# --------------------------------------------------------
#
# Table structure for table 'category'
#

CREATE TABLE category (
   ID int(5) unsigned DEFAULT '0' NOT NULL auto_increment,
   Name varchar(255) NOT NULL,
   Description varchar(255) NOT NULL,
   ParentID int(5) unsigned DEFAULT '0' NOT NULL,
   LImage varchar(128),
   MImage varchar(128),
   SImage varchar(128),
   PRIMARY KEY (ID),
   KEY Description (Description)
);

#
# Dumping data for table 'category'
#

INSERT INTO category VALUES( '2', 'Desktops', '', '1', 'DefaultCatLarge.gif', 'DefaultCatMedium.gif', 'DefaultCatSmall.gif');
INSERT INTO category VALUES( '1', 'Computers', '', '0', 'DefaultCatLarge.gif', 'DefaultCatMedium.gif', 'DefaultCatSmall.gif');
INSERT INTO category VALUES( '3', 'Notebooks', '', '1', 'DefaultCatLarge.gif', 'DefaultCatMedium.gif', 'DefaultCatSmall.gif');
INSERT INTO category VALUES( '14', 'Video Games', '', '0', 'DefaultCatLarge.gif', 'DefaultCatMedium.gif', 'DefaultCatSmall.gif');
INSERT INTO category VALUES( '13', 'Hand Held', '', '1', 'DefaultCatLarge.gif', 'DefaultCatMedium.gif', 'DefaultCatSmall.gif');

################################################################
</pre>

<?
$CS_HELP_EMAIL="help@127.0.0.1";
$CS_SERVERNAME="127.0.0.1";
$CS_SCRIPTNAME=basename($PHP_SELF);
$CS_DBHOST="127.0.0.1";
$CS_DBUSER="blaze";
$CS_DBPASS="johnny";
$CS_DATABASE="test_cs";
$CS_FONTATTRIBUTES="";


function mySiteMap($ShowDescription="",$me=0) {
global $CS_SCRIPTNAME,$CS_DBHOST,$CS_DBUSER,$CS_DBPASS
global $CS_DATABASE,$CS_FONTATTRIBUTES;
global $MSM_QUERY,$IS_SUBMAP;
global $final,$CatName,$CatDescription;

if(!$MSM_QUERY){
mysql_connect($CS_DBHOST,$CS_DBUSER,$CS_DBPASS);
$q=mysql_db_query($CS_DATABASE,"select * from category");
unset($final);
while($result=mysql_fetch_object($q)):
        $final[$result->ParentID][] = $result->ID;
        $CatName[$result->ID] = $result->Name;
		$CatDescription[$result->ID] = $result->Description;
endwhile;
mysql_free_result($q);
$MSM_QUERY=1;
}

        if (is_array($final[$me])):
			echo '<ul>';		
				print "\n";
                while (list(,$child)=each($final[$me])):
                if($me==0){   // Top Level Category
				echo '<br><li type=DISC>';
				echo '<a href="showMyCats.php3?InID='.$child.'">'.$CatName[$child].'</a>';
				echo '</li>';
				print "\n";
				}else{  // NOT Top Level Category, A Sub Cat
				echo '<li type=CIRCLE>';
				echo '<a href="showMyCats.php3?InID='.$child.'">'.$CatName[$child].'</a>';
				echo '</li>';
				echo '<br>';
				print "\n";
				}
				mySiteMap($ShowDescription,$child);
				echo "";
                endwhile;
                echo "</ul>\n";
        endif;
}


mySiteMap();

?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-08-06 13:52 UTC] zeev at cvs dot php dot net
This is an undocumented incompatibility with PHP 3.0, for
an undocumented behavior of unset().

unset() in PHP 3.0 caused the variable, and any variables
that may have pointed to it, to become unset.  There wasn't
much of a notion of references in PHP 3.0, but you could
still have variables pointing at each other in two cases -
global variables (local variables pointing at global variables) and variables that are passed by reference.  This behavior was a side effect of implementation.

With PHP 4.0, the definition of unset() is very strict.  Simply put - it means 'remove that symbol'.  So, in your code, unset($final) tells PHP to remove the symbol 'final', which is a local symbol pointing to the global symbol with the same name.  What happens is that only the local reference gets deleted, and throughout the rest of the function, whenever you reference $final, you reference a local variable.

If you actually want to unset() the global symbol $final, you have to unset $GLOBALS["final"] instead.  But in your case, I think you can do with simply assigning "" into $final (or rely on the default value, which is "" anyway).

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 03:01:28 2024 UTC