shergold Posted August 26, 2009 Share Posted August 26, 2009 Hey guys, i was just wondering if anyone could point me in the right direction. Im trying to update the value of a variable in a function then use it outside of the function , for example; <?php function update(){ $tile = "grass"; } echo $grass; ?> I have tried using return $grass at the end of the function but it still isnt working. Thanks, shergold. Quote Link to comment https://forums.phpfreaks.com/topic/171994-solved-function-variable/ Share on other sites More sharing options...
Asheeown Posted August 26, 2009 Share Posted August 26, 2009 <?php function update() { $tile = "grass"; return $tile; } echo update(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/171994-solved-function-variable/#findComment-906895 Share on other sites More sharing options...
shergold Posted August 26, 2009 Author Share Posted August 26, 2009 it still isnt working, heres my actual code function tile(){ $tile = "grass"; return $grass; } function render($rows = 10 , $cols = 10 , $tileWidth = 50 , $tileHeight = 50 ){ $gameWidth = $cols * $tileWidth; $gameHeight = $rows * $tileHeight; $game = "<table frame=\"border\" width=\"$gameWidth\" height=\"$gameHeight\" cellpadding=\"0\" cellspacing=\"0\">\n"; // while there is more tiles to display keep looping for ($i = 0; $i < $rows; ++$i) { $game .= "<tr>\n"; for ($j = 0; $j < $cols; ++$j) { //if user location is equal to tile location display user if ($i == $y && $j == $x) $game .= "<td width=\"$tileWidth\" height=\"$tileHeight\" background=\"" . tile() . ".gif\"><img src=\"mod.gif\" alt=\"tile\">\n</td>\n"; //else display no user else $game .= "<td width=\"$tileWidth\" height=\"$tileHeight\" background=\"" . tile() . "gif\">\n</td>\n"; if you cant put tile() in the middle of a variable is there anyway i can put the value of tile() in there? Quote Link to comment https://forums.phpfreaks.com/topic/171994-solved-function-variable/#findComment-906902 Share on other sites More sharing options...
Asheeown Posted August 26, 2009 Share Posted August 26, 2009 function tile(){ $tile = "grass"; return $grass; } $t = tile(); Why are you returning $grass, that variable doesn't even exist $tile is the actual variable function tile(){ $tile = "grass"; return $tile; } $t = tile(); Quote Link to comment https://forums.phpfreaks.com/topic/171994-solved-function-variable/#findComment-906904 Share on other sites More sharing options...
shergold Posted August 26, 2009 Author Share Posted August 26, 2009 what do you mean? $tile is the variable and grass is the value of the variable. Quote Link to comment https://forums.phpfreaks.com/topic/171994-solved-function-variable/#findComment-906905 Share on other sites More sharing options...
mikesta707 Posted August 26, 2009 Share Posted August 26, 2009 you are returning the wrong variable. the variable $grass is nonexistent in the function, until you return it, which instantiates it with a value of null. You have to return the variable $tile. you function should be function tile(){ $tile = "grass"; return $tile; } Quote Link to comment https://forums.phpfreaks.com/topic/171994-solved-function-variable/#findComment-906906 Share on other sites More sharing options...
shergold Posted August 26, 2009 Author Share Posted August 26, 2009 ahhh sorry lol... see it now hehe, accident. Quote Link to comment https://forums.phpfreaks.com/topic/171994-solved-function-variable/#findComment-906907 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.