Jump to content

Calling variables defined inside a function?


sorenchr

Recommended Posts

Hi there

 

Im having a troubled time wrapping my head around this problem, when i echo $testvar at the bottom line, it leaves me blank in the explorer.

 

function setVar($var, $testvar, $changeto) {

	if($var == '1') {

		$testvar = $changeto;

	}

	elseif($var == '0') {

		$testvar = '0';

	}

	else {

		echo "Error.";
		exit;

	}

}

setVar(1, $admin, norights);
echo $admin;

 

When i echo the $testvar inside the if($var == '1'), it prints it fine. I've also tried globalizing $testvar inside the if($var == '1') brackets, that doesn't seem to help either. Can anybody help me with this?

Maybe it would be better if you return the variable. Might be better to set the match as a number instead of a string. Echoing the $admin variable won't do anything because its empty. You could store the function result in a variable and echo the variable instead.

The purpose of functions are to accept some input (the parameters passed when the function is called, if any), perform some function, and return (or output) the results. Variables that are used inside of a function are only available in that function (as soon as the function ends, those variables are destroyed.) This is referred to as variable scope.

 

If you want a variable that exists inside of a function to be available later, than you need to be using an OOP class.

you can define variables as global inside a function, and that will cause the function to use the variable outside of it's scope while providing access to same variable to others outside of the function. example:

 

$something = 21;

function do_something()
      global $something;
      $something++;
}

do_something();

echo $something;

 

output:

 

22

you can define variables as global inside a function, and that will cause the function to use the variable outside of it's scope while providing access to same variable to others outside of the function. example:

 

$something = 21;

function do_something()
      global $something;
      $something++;
}

do_something();

echo $something;

 

output:

 

22

 

Globals are going away in php6 this should not be a method that anyone is learning anymore.

Register_globals are going away in php6. That is not the same as using the global keyword.

 

Writing functions that rely on the global keyword to pass values is poor programming. Consider the case of all the php built-in functions. None of them require you to setup a specifically named variable in your main code so that the function can use it.

 

If you are writing functions that need to use the global keyword to work, either you should be using OOP classes, because you have related variables and functions that belong together or you should not be using a function and just copy/paste your code where it is needed.

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.