How to pass a $_GET
variable into function?
$_GET['TEST']='some word';
public function example() {
//pass $_GET['TEST'] into here
}
When I try to access $_GET['TEST']
in my function, it开发者_运维百科 is empty.
The $_GET
array is one of PHPs superglobals so you can use it as-is within the function:
public function example() {
print $_GET['TEST'];
}
In general, you pass a variable (argument) like so:
public function example($arg1) {
print $arg1;
}
example($myNonGlobalVar);
If this is a function and not an object method then you pass the parameter like so
function example($test) {
echo $test;
}
and then you call that function like so
$_GET['test'] = 'test';
example($_GET['test']);
output being
test
However if this is an object you could do this
class Test {
public function example($test) {
echo $test;
}
}
and you would then call it like so
$_GET['test'] = 'test';
$testObj = new Test;
$testObj->example($_GET['test']);
and the output should be
test
I hope this helps you out.
First of all - you should not set anything to superglobals ($_GET
, $_POST
, etc).
So we convert it to:
$test = 'some word';
And if you want to pass it to the function just do something like:
function example($value) {
echo $value;
}
And call this function with:
example($test);
function example ($value) {
$value; // available here
}
example($_GET['TEST']);
function example($parameter)
{
do something with $parameter;
}
$variable = 'some word';
example($variable);
Simply declare the value for the variable by
declare the function by
function employee($name,$email) {
// function statements
}
$name = $_GET["name"];
$email = $_GET["email"];
calling the function by
employee($name,$email);
精彩评论