Function_A calls function_B.

Can you please tell me, is it possible, somehow inside the function_B, to determine which function caused it?

And another situation is of interest, and if, for example, instead of function_A, there is just a script, is it possible to somehow define it, so that for example it shows "main".

    1 answer 1

    Usually, for this purpose, the call stack is obtained using the debug_backtrace() function. The first element will contain information about the current function, the second - about the immediate parent. For example:

     <?php function b($str) { echo "From b: $str<br >"; $funcs = debug_backtrace(); echo "Parent: ". $funcs[1]['function']; } function a($str) { b($str); } a('Hello world!'); 

    Result

     From b: Hello world! Parent: a