Scott
Scott

Reputation: 5379

Tell How Function was called

I've looked at debug_backtrace but so far it doesn't do what I need it to do.

I need to know whether the function I'm calling was 'called' or 'echo-ed'. Like this:

function hello() {
    //blah blah
}

echo hello(); //echo-ed
hello(); //'called'

But the function would do different things if it was 'called' over 'echo-ed'.

How would I do that?

Upvotes: 2

Views: 163

Answers (3)

Frederik Kammer
Frederik Kammer

Reputation: 3177

There is no efficient way to deal it

Update: There is no way to deal it :)

Upvotes: 1

fresskoma
fresskoma

Reputation: 25791

I am pretty sure that this is impossible. The reason this cannot work is that "echo" or any other operator, function or variable assignment uses the return value of the function you've called. So if you've got the following:

echo function1();

What happens is that function1 gets executed, and the return value is passed to echo. Therefor, function1 cannot possibly know that its return value is going to be "echo-ed", because by the time that happens, function1() has already been called and finished executing.

Upvotes: 5

fsonmezay
fsonmezay

Reputation: 538

two examples to help you understand.

function hello(){
  return "Hello!";
}
echo hello(); // prints Hello!


function hello(){
  echo "Hello!";
}
hello(); // prints Hello!

Upvotes: 0

Related Questions