Question

Check out this code:

$last = end($p = explode('/', $someString));  

Getting this notice:

Only variables should be passed by reference

I'm really confused because $p is a variable.

Was it helpful?

Solution 2

The function end() expects a real variable and not a function returning an array, but if you put the function return inside double parentheses PHP does not report a strict standards notice:

$last = end( ( explode( '/', $someString ) ) );

OTHER TIPS

end() expects a variable, not a reference. In your exemple $p = explode('/', $someString) is not a variable, it's an assignment. As the documentation said:

This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.

You should do this instead:

$p = explode('/', $someString);
$last = end($p); 

The problem is that you're doing assignment to a function, and the value being passed to $last is actually the result of the function, not $p. And, explode returns a reference to a variable.

Do it in two lines:

$p = explode('/', $someString);
$last = end($p);  

There's no reason to be assigning to $p inside the function call of end(), unless you're using $p later. And for stylistic clarity I'd suggest doing it in two steps anyhow.

Functions like end() will affect the variable and change their values if they're supposed to, and as all of us know, it means Pass by Reference! But this kind of passing needs tendency, we can't just push things to them without any care!

On giving Only variables should be passed by reference actually the problem is not that variable, however you did it when tried end($p = explode('/', $someString)); but the kind of passing it to the function, because you're not allowed to make a reference on that on calling end(), you made an Expression as an input not a variable.

Expressions:

PHP takes expressions much further, in the same way many other languages do. PHP is an expression-oriented language, in the sense that almost everything is an expression. Consider the example we've already dealt with, $a = 5. It's easy to see that there are two values involved here, the value of the integer constant 5, and the value of $a which is being updated to 5 as well. But the truth is that there's one additional value involved here, and that's the value of the assignment itself. The assignment itself evaluates to the assigned value, in this case 5. In practice, it means that $a = 5, regardless of what it does, is an expression with the value 5. Thus, writing something like $b = ($a = 5) is like writing $a = 5; $b = 5; (a semicolon marks the end of a statement). Since assignments are parsed in a right to left order, you can also write $b = $a = 5.

That error is just a kind of strict error at your case and not a fatal one! well you know that PHP is a strict programming language. But you can disable them manually. That doesn't change the result.

Some examples on this issue:

function foo($a)
{
    return ++$a;
};
$b = 1;
echo foo(&$b);

Error:

Fatal error: Call-time pass-by-reference has been removed; If you would like to pass argument by reference, modify the declaration of foo() since PHP 5.3.0

Or

function foo(&$a)
{
    return ++$a;
};
echo foo($b = 1); // output: 2

We've seen it before Strict Standards: Only variables should be passed by reference

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top