Only variables should be passed by reference ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Only variables should be passed by reference ??

Post by Stryks »

I've just upped my development environment to strict notice reporting, and I'm finding what seems a weird error popping up. Probably simple ... it's late and I'm sure I've got some fuzzy thinking going on.

The following code is giving "ERROR: E_Strict - Only variables should be passed by reference.

Code: Select all

$CURRENT_PAGE = array("NAME"=>end(explode("/",$_SERVER["SCRIPT_NAME"])), "AUTH_LEVEL"=>0, "SSL"=>false);
Am I missing something here?

Thanks
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Only variables should be passed by reference ??

Post by Stryks »

Okay ... changing that to ...

Code: Select all

$CURRENT_PAGE = "";
$CURRENT_PAGE = array("NAME"=>(end(explode("/",$_SERVER["SCRIPT_NAME"]))), "AUTH_LEVEL"=>0, "SSL"=>false);
... fixed it.

Not really sure what the significance of that is.

Can anyone explain this ... and why it might be important?

Cheers all. :)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Only variables should be passed by reference ??

Post by alex.barylski »

Weird. Not the error so much as the solution to fix it.

Code: Select all

end(explode("/",$_SERVER["SCRIPT_NAME"]))
end() expects an array by reference but it's getting the results of a function which while it's returning an array you cannot pass a function result as an argument to a function that is expecting a variable which it can then reference.

Basically end() is expecting a reference to a variable which it can then adjust accordingly but because you pass in a function which returns a temporary variable (it's not available in the context of end()) end() cannot actually update the reference properly and thus you are getting an error.

Code: Select all

 
$tmp = explode("/", $_SERVER["SCRIPT_NAME"]);
end($tmp);
 
This will correct the error -- correctly!!! :)
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Only variables should be passed by reference ??

Post by Stryks »

Ahh .... NOW it makes sense.

And you're right ... the workaround I fluked IS weird. Anyhow, I understand how to do it correctly now.

Many thanks. :D
Post Reply