#48937 [Opn]: Strict standards: Only variables should be passed by reference

From: Date: Thu, 16 Jul 2009 16:17:11 +0000
Subject: #48937 [Opn]: Strict standards: Only variables should be passed by reference
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-2500@lists.php.net to get a copy of this message
ID: 48937 Updated by: rasmus@php.net Reported By: software at whatsonyourbrain dot com Status: Open Bug Type: Documentation problem Operating System: Win XP SP3 PHP Version: 5.3.0 New Comment: Note that this is not a new thing in 5.3. This has been the case for a couple of years. And your explanation is a bit misleading. You say "the type must not be an array but a variable". That makes no sense because it is contradictory. $path = array(1,2,3); end($path); Here $path is an array and it is, of course, also a variable. The explanation really is as simple as the warning indicates. You can only pass variables by reference. The type of the variable is irrelevant to this. If you pass something by reference that isn't a variable, then the called function has no place to store its modifications. It worked in old versions of PHP without a warning, but it actually led to memory corruptions in some cases, so technically this has never worked. Previous Comments: ------------------------------------------------------------------------ [2009-07-16 14:35:23] software at whatsonyourbrain dot com right. But the reader needs to understand why. The answer: Because, only VARIABLES may be passed by reference, as the error clearly states. What is missing here-- for the benefit of others relying upon The Manual for a proper reference-- is a deeper explanation of the reason this error occurs. This is a PHP "type juggling" issue [ http://php.net/manual/de/language.types.type-juggling.php ] REASON FOR ERROR: end() is able to extrapolate what TYPE CAST is passed to it, based on the context of the procedure (e.g. a Variable, or an Array). The problem here arises due to passing-by-reference loosely-typed data. To process loosely-typed data w/out error in this instance-- because the data in end() [and other built-in-functions] is Passed-By-Reference, the TYPE must NOT be an ARRAY, but a VARIABLE. Although previous versions of PHP were processing data no matter how the TYPE was CAST, this is no longer the case. When considering what to pass to "end()", no longer may the programmer disregard that data type (i.e. It is important to know when TYPE CASTING data, whether the type cast is an Array, String, Binary, Float, etc., and ALSO precisely HOW a function will handle the data it is passed) SPECIFICALLY: In this example, "explode()" returns data of type Array. Versions of PHP pervious to __(?)__ would process data passed to end(), regardless of its TYPE, without error. Since PHP version __?__, Strict Standards require that only Variables are passed by reference. To pass data to end using explode() is incompatible in this instance, due to the TYPE CASTING of explode() as Array. This explains the use of a /temporary/ variable, $parts, in the example provided [@16 Jul 1:42pm UTC]. ------------------------------------------------------------------------ [2009-07-16 13:42:32] rasmus@php.net Not at all, simply do: $parts = explode('/',$item); $itemsArray[]= end($parts); If you want to use end() in this case. ------------------------------------------------------------------------ [2009-07-16 13:36:50] software at whatsonyourbrain dot com What is the proper way of coding with end(), for strict standards-- so that its use is not erroneous? It would seem the instructions provided [@16 Jul 2:56am UTC] are directing the user to avoid use of "end()". Should I assume that "end()" itself will be deprecated? ------------------------------------------------------------------------ [2009-07-16 02:56:22] rasmus@php.net Converting to an array doesn't make much sense here. You are just doing simple string stuff. I would change it to: $itemsArray[]= substr(strrchr($item,'/'),1); ------------------------------------------------------------------------ [2009-07-16 01:59:05] software at whatsonyourbrain dot com please advise [@16 Jul 12:56am]. (i.e. what is the proper way of coding this procedure, or-- what is the proper way of coding this procedure to avoid error?) ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/48937 -- Edit this bug report at http://bugs.php.net/?id=48937&edit=1

« previous php.doc.bugs (#2500) next »