Function Help

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
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Function Help

Post 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
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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');
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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).
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

Fixed the problem, thanks!
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post 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');

?>
Post Reply