Reputation: 16575
index.php
require '../include/FunctionFile.php';
$test = "blah";
myfunction($test);
FunctionFile.php
function myfunction($test){
global $test;
echo $test;
}
I want to pass $test
value to myfunction but look like it's not working, it's return nothing, nothing in error log.
Upvotes: 0
Views: 64
Reputation: 118
You can also try this
myfunction("args");
function myfunction($test){
echo $test;
}
Upvotes: 0
Reputation: 16117
I know other mate already provided the solution, so i am adding my answer for future aspects.
Suppose you have two functions getHello()
and getGoodbye()
with different definition same purpose.
// function one
function getHello(){
return "Hello";
}
// function two
function getGoodbye(){
echo "Goodbye";
}
//now call getHello() function
$helloVar = getHello();
Result:
'Hello' // return 'hello' and stored value in $helloVar
//now call getGoodbye() function
$goodbyeVar = getGoodbye();
Result:
'Goodbye' // echo 'Goodbye' and not stored in $goodbyeVar
echo $helloVar; // "Hello"
echo $goodbyeVar; // Goodbye
Result:
'GoodbyeHello'
// now try same example with this:
echo $helloVar; // "Hello"
//echo $goodbyeVar; // Goodbye
Result should be same because getGoodbye()
already echo'ed
the result.
Now Example with Your Code:
function myfunction($test){
//global $test;
echo $test;
}
function myfunction2($test){
//global $test;
return $test;
}
myfunction('test'); // test
myfunction2('test'); // noting
//You need to echo myfunction2() as i mentioned in above.
echo myfunction2('test'); // test
Why it's not working in your code?:
you need to declare variable as Global
before assigning the values like:
global $test;
$test = "blah";
Upvotes: 1
Reputation: 22532
Your function need return
value.
index.php
require '../include/FunctionFile.php';
$test = "blah";
$var=myfunction($test);// assign to vaiable
echo $var;
FunctionFile.php
function myfunction($test){
return $test;// use return type here
}
Upvotes: 2