Jump to content

[SOLVED] Function variable


shergold

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/171994-solved-function-variable/
Share on other sites

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?

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.