Reputation: 1090
A while back, I wrote a website in php, and am now setting it up on my webserver. Back when I wrote it, everything was fine, but now, it is generating these errors:
Strict Standards: Only variables should be passed by reference in /home/dragonto/public_html/source/navbar.php on line 4
class="current">Home Strict Standards: Only variables should be passed by reference in /home/dragonto/public_html/source/navbar.php on line 4
>About Strict Standards: Only variables should be passed by reference in /home/dragonto/public_html/source/navbar.php on line 4
>Contact Us Strict Standards: Only variables should be passed by reference in /home/dragonto/public_html/source/navbar.php on line 4
>Projects Strict Standards: Only variables should be passed by reference in /home/dragonto/public_html/source/navbar.php on line 4
>Tutorials
I'm not sure what these mean. I did research on passing by ref in PHP, and it doesn't look I made any violations. Here is the script in question:
<?php
function GetScriptName()
{
return end(explode("/", $_SERVER['SCRIPT_FILENAME']));
}
$Links = array(
1 => array(1 => "Home", 2 => "index.php"),
2 => array(1 => "About", 2 => "about.php"),
3 => array(1 => "Contact Us", 2 => "contact.php"),
4 => array(1 => "Projects", 2 => "projects.php"),
5 => array(1 => "Tutorials", 2 => "tutorials.php")
);
echo "<nav>\n<ul>\n";
foreach($Links as &$Link)
{
echo "<li";
if($Link[2] == GetScriptName())
echo " class=\"current\"";
echo "><a href=\"" . $Link[2] . "\">" . $Link[1] . "</a></li>\n";
}
echo "</ul>\n</nav>\n";
?>
Upvotes: 0
Views: 61
Reputation: 437336
The problem is with the call to end
, which takes its argument by reference. But in this snippet the actual argument is not a variable, so it cannot be passed by reference. The strict standards warning complains about that.
This warning is quite benign and while you should not have production code generating warnings under any circumstances, in practice you can ignore it (the code still works as intended).
But it's also very very easy to shut it up, by introducing a variable manually:
$arr = explode("/", $_SERVER['SCRIPT_FILENAME']);
return end($arr);
In this particular case it might be better to fix the issue by using something other than end
-- something that is also more semantically accurate such as
return basename($_SERVER['SCRIPT_FILENAME']);
Upvotes: 7