Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.