Page 1 of 1

Function Help

Posted: Wed Oct 01, 2003 12:07 pm
by php_wiz_kid
What's wrong with this function?

Code: Select all

<?php
function menuButton($menuNum, &$buttonName) {
	$menuButton = 
	"<div class="menuBar"><a class="menuButton"
    href="#"
    onclick="return buttonClick(event, 'menu$menuNum');"
    onmouseover="buttonMouseover(event, 'menu$menuNum');">$buttonName</a></div>";
}
echo $menuButton
?>
I just get the error
Fatal error: Cannot pass parameter 2 by reference in C:\Apache2\htdocs\index.php on line 5

Posted: Wed Oct 01, 2003 12:10 pm
by Stoneguard
Remove the ampersand in the input parameter

also, if you want to return the value of the function, use return. The function should look more like:

Code: Select all

<?php
function menuButton($menuNum, $buttonName) { 
   $str = 
   "<div class="menuBar"><a class="menuButton" 
    href="#" 
    onclick="return buttonClick(event, 'menu$menuNum');" 
    onmouseover="buttonMouseover(event, 'menu$menuNum');">$buttonName</a></div>"; 
} 


echo menuButton (0, 'test');
?>

Posted: Wed Oct 01, 2003 12:12 pm
by volka
what does the code calling this function look like?
Is it really necessary to have $buttonname as reference? You're not altering it.
Why don't you return the string instead of assigning it to a local variable? $menuButton is gone when function menuButton returns unless you mark it as global (or static).

Posted: Wed Oct 01, 2003 12:13 pm
by php_wiz_kid
Fixed the problem, thanks!

Posted: Wed Oct 01, 2003 12:23 pm
by Derfel Cadarn
Every call for this function returns the same link! Shouldn't there be a third var in the function to present the target?
Something like:

Code: Select all

<?php
function menuButton($menuNum, $buttonName, $target) {
   $str =
   "\n<div class="menuBar"><a class="menuButton"
    href="$target"
    onclick="return buttonClick(event, 'menu$menuNum');"
    onmouseover="buttonMouseover(event, 'menu$menuNum');">$buttonName</a></div>";

return($str);
}


echo menuButton (0, 'test', 'index.html');
echo menuButton (1, 'test1','some_other_page.html');

?>