note 38128 added to function.include

From: Date: Tue, 09 Dec 2003 15:03:01 +0000
Subject: note 38128 added to function.include
Groups: php.notes 
Request: Send a blank email to php-notes+get-61688@lists.php.net to get a copy of this message
While you can return a value from an included file, and receive the value as you would expect, you do not seem to be able to return a reference in any way (except in array, references are always preserved in arrays). For example, we have two files, file 1.php contains... <?php function &x(&$y) { return include(dirname(__FILE__) . '/2.php'); } $z = "FOO\n"; $z2 = &x($z); echo $z2; $z = "NOO\n"; echo $z2; ?> and file 2.php contains... <?php return $y; ?> calling 1.php will produce FOO FOO i.e the reference passed to x() is broken on it's way out of the include() Neither can you do something like <?php $foo =& include(....); ?> as that's a parse error (include is not a real function, so can't take a reference in that case). And you also can't do <?php return &$foo ?> in the included file (parse error again, nothing to assign the reference too). The only solutions are to set a variable with the reference which the including code can then return itself, or return an array with the reference inside. --- James Sleeman http://www.gogo.co.nz/ ---- Manual Page -- http://www.php.net/manual/en/function.include.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38128 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38128&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38128&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#61688) next »