0

Does PHP interpret code in the same way that JavaScript does? Upon some research, I've gathered that JavaScript programs are run in a so-called 'two-pass' read. The first run gets the syntax and function definitions of the program. ('Parses' the data so to speak), and the second run - well - runs the data. Does PHP work the same way? If not, how does PHP interpret code?

What are the general functions of a PHP interpreter?

  • PHP is precompiling the scripts (these results can be cached for example xcache (opcode caching)) and then run. Is all I know.. – Daniel W. Nov 29 '13 at 13:10
  • 1
    How it runs is rather implementation specific. How Javascript itself runs differs greatly per implementation. PHP has multiple implementations as well. – Dykam Nov 29 '13 at 13:12
  • I'm sorry, I thought that PHP was PHP. Are you suggesting that the settings in PHP.ini can change how PHP programs are interpreted? – user3048961 Nov 29 '13 at 13:14
  • 2
    The fact that you can call functions that are declared later in the code lets me assume that it works in a similar way. I don't know that, though. That's just a thought. – Butt4cak3 Nov 29 '13 at 13:15

2 Answers2

1

Most programming languages work that way, maybe save for batch files. The source code is parsed into tokens and a syntax tree is created which is then evaluated. These are three separate steps and it's practically a lot simpler to keep them separated. If you'd want to mush them together so code got executed while it is being parsed, that means the parser would have to read just enough to get one full block of something which is executable, then hand that over to the runtime, which would then have to hand control back to the parser. It's a lot easier to do everything one by one.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Cool, now I can create more flexible code without scratching my head wondering how the code runs in the first place. – user3048961 Nov 29 '13 at 13:25
0

What you've described in the question is a very tiny aspect of how the interpreter works.

PHP does indeed do this: you can tell because it is valid to have a function call higher up in the code than the actual definition of the function that is being called.

<?php
myFunction();    //this is only valid here before the function itself because of two-pass parsing.

function myFunction() {
    .....
}

myFunction();    //in a single-pass system, the function call would have to be here, after the function itself.
?>

If you only had one pass, you would only be able to call a function after the function itself had been defined. Some languages do work this way (C and Pascal are good examples). These languages require the use of header files if you want write a function call earlier in the code than the function itself is defined.

The difference is that those languages are compiled, which means that the code is only actually run once everything has been built into an executable. The single-pass approach with header files wouldn't work in an interpreted environment because while the header may allow the compiler to accept the function call as valid, an interpeter would still fail because it simply wouldn't have the function available to call.

For this reason, pretty much any interpreted language is going to use this two-pass mechanism.

However, as I said, this is just a small part of the overall design of an interpreter.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • Thanks, but this little bit will still take a lot of confusion away from less-than-completely-experienced programmers like me. Because I was one of those that kind of figured that all programs ran more or less like C- once over. (They're run by machines after all). – user3048961 Nov 29 '13 at 13:23
  • There's no inherent reason for compiled languages to work that way or for interpreted languages not to use header files. Heck, PHP can be *compiled*. An interpreter is just an on-the-fly compiler. – deceze Nov 29 '13 at 13:24
  • PHP can be compiled? That sounds so cool - and out of my league right now lol. Maybe someday after I fully learn C# I'll be able to understand it. – user3048961 Nov 29 '13 at 13:41