Thanase
Thanase

Reputation: 11

Function inside a function: why does this echo statement only display once?

In the following code, why does the phrase "example" only output once if both foo() and bar() are called>

<?php
function foo() 
{
    function bar() 
    {
        echo "example\n";
    }
}

foo();

bar();

?>

Upvotes: 0

Views: 22

Answers (1)

Spholt
Spholt

Reputation: 4012

You want to be declaring your functions separately like so:

<?php
function foo() 
{
    bar();
}

function bar() 
{
    echo "example\n";
}

foo();

bar();

?>

Nesting your functions inside each other doesn't really serve any purpose that i can think of.

Upvotes: 1

Related Questions