php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #20739 parentheses surrounding return()'s arguments do seem to matter
Submitted: 2002-11-30 15:37 UTC Modified: 2003-08-15 06:23 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: c dot gerlach at sympatico dot ca Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.0-dev/4.4.0-dev OS: All
Private report: No CVE-ID: None
 [2002-11-30 15:37 UTC] c dot gerlach at sympatico dot ca
I'm using PHP 4.2.3 on Windows NT 5.0 build 2195 (win2k)
along with Apache and stumbled across a strange behaviour
regarding return() arguments in parentheses.

Consider the following code:

class Node {
  var $children;
  var $name;
	
  function Node($name) {
    $this->children = array();
    $this->name = $name;
  }
	
  function &add($n) {
    $this->children[0] = $n;
    return($this->children[0]); # these () make a difference
  }
}

$t = new Node("top");
$node = new Node("foo");
$n = &$t->add($node);
$n->name = "bar";
print $n->name;               # output: "bar"
print $t->children[0]->name;  # output: "foo"

The code is working as expected (i.e., both times
"bar" is printed) when you remove the
parentheses from the return call in the add method.

According to the manual, parentheses in a return
call shouldn't make a difference.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-30 16:00 UTC] iliaa@php.net
In addition to returning the wrong value the code produces a number of memory leaks that are listed below.
/home/rei/PHP_CVS/php4/Zend/zend_execute.c(468) :  Freeing 0x084F7E24 (12 bytes), script=r.php
/home/rei/PHP_CVS/php4/Zend/zend_hash.c(262) :  Freeing 0x084F7DCC (40 bytes), script=r.php
Last leak repeated 1 time
/home/rei/PHP_CVS/php4/Zend/zend_hash.c(178) :  Freeing 0x084FCBF4 (32 bytes), script=r.php
Last leak repeated 1 time
/home/rei/PHP_CVS/php4/Zend/zend_execute.c(1692) :  Freeing 0x084FCB94 (44 bytes), script=r.php
/home/rei/PHP_CVS/php4/Zend/zend_variables.c(135) : Actual location (location was relayed)
Last leak repeated 1 time
/home/rei/PHP_CVS/php4/Zend/zend_execute.c(471) :  Freeing 0x084F96FC (4 bytes), script=r.php
/home/rei/PHP_CVS/php4/Zend/zend_variables.c(111) : Actual location (location was relayed)
/home/rei/PHP_CVS/php4/Zend/zend_execute.c(479) :  Freeing 0x084F96BC (12 bytes), script=r.php
/home/rei/PHP_CVS/php4/Zend/zend_execute.c(1986) :  Freeing 0x084F960C (44 bytes), script=r.php
/home/rei/PHP_CVS/php4/Zend/zend_API.c(565) : Actual location (location was relayed)
 [2002-12-09 06:41 UTC] andi@php.net
You are right they do matter. When you're putting paratheses you're telling the engine that this is an expression with a possible value (e.g. (3+2) ) and not a variable.
 [2002-12-09 07:14 UTC] sniper@php.net
The docs say different thing:

"Note:  Note that since return() is a language construct and not a function, the parentheses surrounding its arguments are not required--in fact, it is more common to leave them out than to use them, although it doesn't matter one way or the other. "

This is not true, according to Andi..

 [2003-08-15 06:23 UTC] ali@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 17 05:01:27 2024 UTC