0

I have the following line in a file named config.php:

$objects["settings"]["template"]["value"] = "yes";

Then a file named funcs.php which includes config.php, with the following lines.

echo $objects["settings"]["template"]["value"];
function testfunction() {
    echo "<br />function output: ".$objects["settings"]["template"]["value"];
}

I then have index.php which includes funcs.php, as follows.

require_once("funcs.php");
testfunction();

How come the output from the function doesn't read the variables and they are empty?

yes
function output:

The output im expecting is:

yes
function output: yes

Thanks in advance.

EDIT: I went with Yoda's answer as it was the most detailed answer regarding passing $objects as a parameter. Thanks to everyone else who answered!

5
  • 8
    variable scope issue: php.net/manual/en/language.variables.scope.php Commented Apr 21, 2014 at 21:21
  • Your testfunction doesn't have access to the $objects variable. Commented Apr 21, 2014 at 21:21
  • Turn on error reporting. Put error_reporting(-1); at the top of your script. Commented Apr 21, 2014 at 21:23
  • tip, its better to use return inside the function and echo outside. Commented Apr 21, 2014 at 21:24
  • One solution would be to add the line global $objects; inside your function. Another woudl be to use the $GLOBALS array i.e. $GLOBALS['objects']["settings"]["template"]["value"]' Commented Apr 21, 2014 at 21:25

4 Answers 4

2

Simply pass the object as parameter

function testfunction($objects) {
    echo "<br />function output: ".$objects["settings"]["template"]["value"];
}
Sign up to request clarification or add additional context in comments.

Comments

1

The variable is not in function scope. You can achieve that by using global keyword. However doing it like this is bad practice and you should rather pass the value to function through function parameter.

Use of global (bad practice):

$objects["settings"]["template"]["value"] = "yes";

function testfunction() {
    global $objects;        
    echo "<br />function output: ".$objects["settings"]["template"]["value"];
}

testfunction();

Passing value through function parameter:

$objects["settings"]["template"]["value"] = "yes";

function testfunction($objects) {   
    echo "<br />function output: ".$objects["settings"]["template"]["value"];
}

testfunction($objects);

1 Comment

Thanks, this was the most detailed answer of the answers suggesting to pass the object as a parameter which is what I have now done. Thanks to everyone that answered!
1

The variable is outside the scope of the function. To make the variable available in the function, you have to pass is an argument into the function. You will also need to modify the function definition. See sample below

// function definition
function testFunction($variable) {
    echo $variable;
}

// call the function
$your_variable = "Some output";
testFunction($your_variable);

This should give you the expected behaviour.

Comments

-1

Using global can work, but it should only be used if you can't pass the variable through the function as it has some security risk...

$GLOBALS["settings"]["template"]["value"] = 'yes';

This works because it changes the scope of the variable to be global.

$GLOBALS["settings"]["template"]["value"] = 'yes';

function test(){

    echo $GLOBALS["settings"]["template"]["value"];

}

test();

The above prints out yes

7 Comments

posting a don't do it like this answer is not recommended.
@Dagon It's not a don't do it like this answer... it's a do it like this, but a warning answer.
@Arian I dont think it does, try `$GLOBALS['objects']["settings"]["template"]["value"]'
@RiggsFolly I edited my answer, try the code, it works perfectly.
@Arian His question was using $objects["settings"]["template"]["value"] = "yes"; your answer has changed the location of the tested value. Anyway you cannot frig the question to suit YOUR answer after the event.
|

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.