user7750205
user7750205

Reputation: 11

Call function after function return

i have the folowing situation: I have a function "foo" and want the the foo-return the function bar sould be call. Here a little example what i mean:

function foo() {
     echo "FOO";
     return "1";
}
function bar() {
    echo "BAR";
}
echo foo();

And the output should be display this

FOO
1
BAR

All this in PHP!!! Have anyone a idea how to can realise this WITHOUT call bar() in after each foo()?

Upvotes: 0

Views: 5676

Answers (3)

Code_Ninja
Code_Ninja

Reputation: 1867

Or if you have got two functions then, just add foo() like the following:

function bar(){
foo();
echo "BAR";
}

And remove the return statement from foo() function. or write return 0;

Updated:

You can keep the return function in foo().

Upvotes: 0

Philipp
Philipp

Reputation: 15629

What you want isn't possible that way. What you actually ask for is, to automatically execute an statement, after you leave the function AND after you called echo.

Calling an statement after return is possible using finally, but it would be easier (and cleaner) to store the result of calling something else and return it later.

function b() {
    echo "bar";
    return 2;
}

function a() {
    try {
        echo "foo";
        return b();
    }
    finally {
        echo "finally";
    }
}

echo a();

What you really should do, to get your result is just call bar, after you called foo.

echo foo();
bar();

Upvotes: 0

SmashingQuasar
SmashingQuasar

Reputation: 411

You only need to return the call of the function like so:

function foo() {
    echo "FOO";
    return bar();
}

Upvotes: 2

Related Questions