Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3352168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:59:50+00:00 2026-05-18T01:59:50+00:00

Why would PHP allow nesting functions? <?php function foo() { function bar() { return

  • 0

Why would PHP allow nesting functions?

<?php
function foo() {
    function bar() {
        return "bar";
    }
    return "foo";
}
print foo();
print bar();

.. is valid PHP.

But:

  1. Why would nesting be needed ever?
  2. And even if so, why can I call bar from anywhere (and not, e.g. only withing foo(), or trough foo.bar() or such).

I ran into this today, because I forgot a closing bracket somewhere, and had one too many further down. The code was valid and no errors thrown; but it all started acting really weird. Functions not being declared, callbacks going berserk and so on.
Is this a feature, and if so, to what purpose? Or some idiosyncrasy?

ANSWER: commentor points out that this is a duplicate of What are php nested functions for.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:59:50+00:00Added an answer on May 18, 2026 at 1:59 am

    Note that the order is important here; you cannot call bar() before calling foo() in your example. The logic here appears to be that the execution of foo() defines bar() and puts it in global scope, but it’s not defined prior to the execution of foo(), because of the scoping.

    The use here would be a primitive form of function overloading; you can have your bar() function perform different operations depending upon which version of foo() declares it, assuming of course that each different version of foo() indeed defines a bar() function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a PHP class/library that would allow me to query an XHTML document
In PHP how would I allow only donations from PayPal balance and not from
I'm looking into writing an PHP REST API that would allow registered users interact
What are some reasons why PHP would force errors to show, no matter what
I'm programming in PHP and would like to create web pages which have email
I'm not the best at PHP and would be extremely grateful if somebody could
Is anyone aware of a script/class (preferably in PHP) that would parse a given
Why would I want to use PHP's filter library? Why wouldn't I? It seems
We have a PHP project that we would like to version control. Right now
I would like to use my laptop as a web development (PHP, Python, etc.)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.