0

Here is a shortened version of my code:

<?php $foo = "Bar"; function test () { echo $foo; // Undefined variable? } test(); ?> 

I don't understand, why PHP says, that $foo is undefined. And I always find solutions for deactivating the error reporting, but I want to fix my code. What am I missing?

2
  • The variable $foo is outside of the scope if the function test. You would need to make it global - but this is considered bad practice! php.net/manual/en/language.variables.scope.php Commented Feb 2, 2014 at 13:30
  • the comment about deactivating the error reporting is not very clear, but if what you say is that you deactivate error reporting, there is no way to debug your code, you should have it always enabled on your developing system. Commented Feb 2, 2014 at 13:31

5 Answers 5

2

In PHP you cannot access global variables from functions unless you explicitly import them using global $foo;.

$foo = "Bar"; function test() { global $foo; echo $foo; } test(); 

Another option would be accessing it as $GLOBALS['foo'] - $GLOBALS is a special array that "contains" the global scope and is available wverywhere.

However, using global variables is usually a bad idea that results in spaghetti code. Try to avoid them. Usually the correct way is to make the variable a function argument:

function test($foo) { echo $foo; } test('bar'); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you :) I simply forgot that.
1
<?php $foo = "Bar"; function test ($foo) { echo $foo; // Now it's defined, as it's passed to the function. } test($foo); ?> 

It is outside the scope of the function, therefor it does not know it exists.

Don't listen to them telling you to use global - it's not good practice, and will really clutter up your global array (and potentially cause security risks depending on what you toss in there).

Pass the parameters as arguments. This is the correct way.

3 Comments

is not a good practice, but still is the answer to his problem, he may be learning about scopes
The answer to "how can I light a firecracker" should not be simply "use a flame". Safety first.
Thank you too :) I know scopes. I'm programming in C# and C++. I simply forgot this rule in PHP.
1

In the function test, $foo is currently regarded as a local variable. But you didn't defined such a local variable in this method.

What you want, is to use the variable $foo defined outside this function. Hence you want to use a global variable. So you should declare it as such, ie, using global $foo in your function test.

You may want to read http://www.php.net/manual/en/language.variables.scope.php to get a better understanding of this concept.

Comments

-1

add a global $foo on your function

$foo = "Bar"; function test () { global $foo; echo $foo; // Undefined variable? } test(); 

Comments

-1

It seems you need some more tutorials... This is the basics of programming languages: http://www.php.net/manual/en/language.variables.scope.php

You should pass you variable as parameter (in some cases, global variable can be used but rarely).

1 Comment

Thank you too :) I'm programming in multiple languages and I simply forgot this scope rule in PHP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.