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
php_wiz_kid
Forum Contributor
Posts: 181 Joined: Tue Jun 24, 2003 7:33 pm
Post
by php_wiz_kid » Wed Oct 01, 2003 12:07 pm
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 » Wed Oct 01, 2003 12:10 pm
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');
?>
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Oct 01, 2003 12:12 pm
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 » Wed Oct 01, 2003 12:13 pm
Fixed the problem, thanks!
Derfel Cadarn
Forum Contributor
Posts: 193 Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany
Post
by Derfel Cadarn » Wed Oct 01, 2003 12:23 pm
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');
?>