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.
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 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 ) ) );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With