0

How does scope work for functions in PHP? Can I use function A (defined outside function B) from within function B?

2
  • You could have made a simple test and find out by yourself... Commented Mar 25, 2011 at 12:36
  • 2
    You are right, but I had a problem with this in a website but the culprit was a different problem. Anyway even if its is an "easy"question, I am sure it will be useful for somebody googling it, so no need to be so whiny. Commented Mar 25, 2011 at 12:40

3 Answers 3

5

From php manual:

All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.

So yes, you can.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you can do it like this:

function A()
{
  ...
}

function B()
{
  A();
}

B();

Comments

0

Variable scope if for variables.

There is no "scope" for functions... except when using namespaces.


Quoting the User-defined functions section of the manual :

All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.

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.