Reputation: 595
I know it would be easy using methods and attributes, but I can not just do it with functions. It's for a wordpress plugin so I also can not use global variables.
I would like to know how to pass the GET value of the Post()
function to Funend ()
.
My unsuccessful attempt:
<?php
//FUNCTION 1
function Name($a)
{
return $a;
}
//FUNCTION 2
function Post()
{
$city = $_GET['city'];
Name($city);
}
//FUNCTION 3
function Funend()
{
$funend = Name();
///DEBUG
ob_start();
var_dump($funend);
$data = ob_get_clean();
$txt = fopen('bug.txt', 'a');
fwrite($txt, $data);
fclose($txt);
//DEBUG
}
Post();
Funend();
Upvotes: 0
Views: 67
Reputation: 2327
First of all - Functions always in small letters.
If you just wants to pass $_GET value to Funend() you can pass it like this
<?php
//FUNCTION 1
function Name($a){
Funend($a);//Calling Funend()function
}
//FUNCTION 2
function Post(){
Name($_GET['city']); //Calling Name()function
}
//FUNCTION 3
function Funend($city){
///DEBUG
ob_start();
var_dump($city);
$data = ob_get_clean();
$txt = fopen('bug.txt', 'a');
fwrite($txt, $data);
fclose($txt);
//DEBUG
}
Post(); //Calling Post()function
?>
Upvotes: 1
Reputation: 22783
Functions can accept values (called arguments or parameters) and they can return values (simply called return value).
If the objective is to let Funend()
receive a value from Post()
, you could do this:
function Post() {
return $_GET[ 'city' ]; // return value
}
// accepts one (explicit) argument
function Funend( $city ) {
/* do something with $city */
}
Funend( Post() );
That last line is basically a condensed version of:
$city = Post(); // temporarily store the return value of Post() in $city;
Funend( $city ); // pass $city as an argument to Funend();
except it doesn't use the intermediate variable $city
, thereby keeping the outer scope clean of variables.
Another way to do it could be:
function Post() {
$city = $_GET[ 'city' ];
Funend( $city ); // call Funend() from within Post()
return $city; // perhaps return the city as well?
}
function Funend( $city ) {
/* do something with $city */
}
Post(); // now we only have to call Post()
But then Post()
is coupled to Funend()
, which makes it less flexible, because now you can't call Post()
anymore without automatically invoking Funend()
as well.
Upvotes: 2