3

Well I confused by behavior of PHP when parsing a PHP file. I am reading this since long that

The PHP language is interpreted

So I have code

var_dump(function_exists('abc')); exit;
function abc() {
    return;
}

var_dump should print false as per my assumption but it print bool(true).

Can somebody please help me to understand this behavior?

12
  • it's pre-compiled before running, not interpreted while running AFAIK Commented Jan 7, 2016 at 8:00
  • @RST Any ref or link for that ? Commented Jan 7, 2016 at 8:02
  • function abc() was called before exit; (using function_exists) that's why it returns 'true'. Even if you define the function after exit and it is called before exit, it still execute. Commented Jan 7, 2016 at 8:02
  • what I meant was, using function_exists(), the function abc() was called because it searched if that certain function exists. Commented Jan 7, 2016 at 8:04
  • 2
    What you see here is often called "function hoisting". Non-conditional definitions of functions are "hoisted" to the top of the script. Commented Jan 8, 2016 at 14:12

2 Answers 2

2

See this answer.

In short, it is compiled into a type of bytecode at runtime and then interpreted - in doing this, you will have the definitions for your functions available, even if they appear at the very end.

Sign up to request clarification or add additional context in comments.

3 Comments

Okay so finally PHP is not just interpreted as people say?
Well it is, but the code has to be turned into something readable by the interpreter, this bytecodeish form has your code optomized for the interpreter.
Well it make sense. Thanks Matt.
0

function_exist will Checks the list of defined functions, both built-in (internal) and user-defined, for function_name. So, php interpreter checks if the function with the name is defined in the bytecode which is complied before interpreted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.