0

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.

8
  • is your myfunction part of a class? or just simple written function Commented Jan 13, 2016 at 9:14
  • @urfusion no, it's a simple function Commented Jan 13, 2016 at 9:15
  • 1
    are you sure that the file with the functions is being included correctly? Commented Jan 13, 2016 at 9:19
  • is filepath of FunctionFile.php correct? It is working in my end. Commented Jan 13, 2016 at 9:21
  • @RamRaider yes, i'm sure Commented Jan 13, 2016 at 9:25

3 Answers 3

2

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

use this to check error in page ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
apologize for prev reply! it was my wrong, it working fine.. Thanks
nice one @Saty: great answer
1

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

Comments

0

You can also try this

myfunction("args");
function myfunction($test){
	echo $test;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.