2

I have primarily worked on java. Now Learning PHP.

After reading couple of articles on google, i got the understanding but got some doubts too.

Compiled vs Interpreted

Java Compiled language :-

Developer writes helloworld.java, compiles it and run it on JVM. Java needs to be explicitly compiled first by developer and hence said to be compiled language.

PHP Interpreted language :-

Developer writes helloworld.php. PHP engine (like symfony) internally compiles it and iterprets it . PHP need not to be explicitly compiled first by developer and hence said to be interpreted language as for developer written code can be directly interpreted. Is that correct?

Runtime Environment:-

Java :- Program can be executed wherever JRE/JVM is installed. No explicit web server is required.

PHP :- We need the web server(containing PHP engine) like apache to run the PHP program. Can it run without webserver ? Also PHP engine inside web server is platform dependant For example:- Apache server for windows one implementation of PHP engine, while on linux apache will contain different implementation of PHP engine ?

4

Developer writes helloworld.java, compiles it

This is wrong. There is nothing in the Java Language Specification that requires an implementation to be compiled. An interpreted implementation of Java is perfectly possible, valid, and compliant with the specification.

Early versions of the VisualAge for Java IDE had an interpreter that let you experimentally explore snippets of Java code, for example.

Speaking of VisualAge for Java, there is also another hidden assumption in your statement: Java does not require the existence of source files. In VisualAge for Java, for example, packages, classes, interfaces, methods, fields, comments, etc. were stored in a database, not as source files.

and run it on JVM.

This is wrong. There is nothing in the Java Language Specification that requires the existence of a JVM. It is perfectly possible, valid, and specification-compliant to implement Java without a JVM.

Some real-world examples are:

  • Android does not contain a JVM.
  • GWT allows to compile Java to ECMAScript, no JVM involved in running the code.
  • GCJ (now defunct) allowed to compile Java to native machine code, no JVM involved.

There are also some tricky corner cases: Excelsior JET contains a Java-to-native-code Ahead-of-Time compiler that can compile Java source code to native code, but it also contains a JVM that can execute JVM bytecode. The reason is that your code may interact with third-party libraries for which you don't have access to the source code, so libraries for which the source code is not available are executed by the JVM, and libraries for which the source code is available are compiled by the Java compiler.

Java needs to be explicitly compiled first by developer

This is wrong. There is nothing in the Java Language Specification that requires an explicit compilation step triggered by the developer. For example, some modern IDEs will implicitly compile the code while you type.

Also, as I already wrote above, there is nothing in the Java Language Specification that requires compilation at all. Interpretation is equally valid.

and hence said to be compiled language.

This is not even wrong. It is more than wrong, it is non-sensical.

There is no such thing as a "compiled language". A language is a set of abstract mathematical rules and restrictions. It is a piece of paper. (Or, in modern times, a PDF or a web page.)

Compilation and interpretation are traits of the compiler and interpreter (duh!), i.e. the implementation, not the language. A language isn't compiled or interpreted. A language just is.

Every language can be implemented with a compiler and every language can be implemented with an interpreter. The vast majority of languages have both compiled and interpreted implementations. Many modern high-performance language implementations actually combine one or more interpreters and one or more compilers in the same implementation!

For example, there are interpreters for C. Ergo, C must be an interpreted language, right?

And while current versions of V8 contain both an interpreter and several compilers, earlier versions (before 2016) used first only one compiler, then [multiple compilers]. So, for 10 years, ECMAScript code executed by V8 was always compiled straight to native machine code, never to bytecode, and never interpreted. Ergo, ECMAScript must be a natively-compiled language, right?

Compilation and interpretation live in the realm of compilers and interpreters, i.e. language implementations / processors (or whatever you want to call them). This is a completely separate realm from the realm of programming languages.

That's why the statement that "Java is a compiled language" is Not Even Wrong, it is non-sensical. If English were a typed language, "compiled language" would be a type error.

Developer writes helloworld.php.

Unlike Java, PHP does not have a formal written specification, but I suspect that a PHP implementation that does not use files would be perfectly valid. (However, the PHP Language Reference does refer to "files" in some places. Personally, I assume that this is a bug. This paragraph would work just fine if it didn't mention "files" and was talking about "source units" instead, for example.)

PHP engine (like symfony)

Symfony is a Web Development Framework, not a language execution engine. A Java equivalent to Symfony would be something like Play, Stripes, Apache Struts, Apache Tapestry, Vaadin, Apache Wicket, JHipster, or Apache Sling.

PHP execution engines are, for example, HHVM, Peachpie, Quercus, JPHP, IBM P8, Tagua VM, HippyVM, Phalanger, or Zend Engine.

internally compiles it and iterprets it .

Whether a PHP implementation interprets PHP, compiles PHP, compiles PHP to an intermediate format and then interprets that intermediate format, compiles PHP to an intermediate format and then compiles that intermediate format, compiles PHP to an intermediate format, interprets that intermediate format for a while and then compiles parts of that intermediate format which are executed often, or sends the source code to China to have it hand-interpreted by a room full of day laborers, is an internal private implementation detail of that implementation and has nothing to do with the PHP programming language.

For example:

  • Early versions of HHVM (then still called HiPHoP) compiled PHP source code to C++ source code. There was no interpretation, no immediate execution, and the developer had to explicitly kick off the compilation process, and explicitly execute the resulting executable.
  • Peachpie compiles PHP sourcecode to CIL bytecode.
  • Phalanger does the same.
  • P8, Quercus, and JPHP compile PHP to JVM bytecode.

PHP need not to be explicitly compiled first by developer

Whether or not PHP source code needs to be explicitly compiled depends on the implementation (e.g. HiPHoP needed an explicit compilation step), as well as the development environment (it would be trivial to write an IDE that calls HiPHoP every time the source code changes).

and hence said to be interpreted language

There is no such thing as an interpreted language.

Java :- Program can be executed wherever JRE/JVM is installed.

No. A JRE/JVM is neither necessary nor sufficient to execute a Java program.

There are implementations of Java which can produce standalone native executables. These do not need a JVM at all. They might need an installed JRE, but you could just as well embed the JRE into the executable, then it would work without an installed JRE. Ergo, a JVM is not necessary at all, and while a JRE is necessary, an installed JRE is not.

On the flip side, a JRE/JVM is not sufficient to execute a Java program. The JRE/JVM cannot execute Java programs, it does not understand Java, it does not know anything about Java. It only understands JVM bytecode, so it only can execute JVM programs. If you want to execute your Java program with a JVM, you need to compile it to JVM bytecode first, i.e. you need a Java compiler, which is not part of the JVM or JRE, it is part of the JDK.

PHP :- We need the web server(containing PHP engine) like apache to run the PHP program.

No. All you need is either an execution engine that can execute PHP code, or a compiler that can compile PHP code to some other language for which you haven an execution engine.

Can it run without webserver ?

Yes, it can. It would be pretty annoying, if commandline PHP build tools or package manager tools like Composer needed to start a web server every time you call them.

Also PHP engine inside web server is platform dependant For example:- Apache server for windows one implementation of PHP engine, while on linux apache will contain different implementation of PHP engine ?

This really depends on how exactly you define "platform dependent" and "different implementation".

Since you come from the Java world, maybe it is simpler to illustrate this with something I assume you are familiar with: the Oracle OpenJDK HotSpot JVM. It is (mostly) written in highly-portable "low-ceremony" C++, but it still contains significant portions of CPU-dependent, OS-dependent, and OS-CPU-combination-dependent code. Now, does that mean it is platform-dependent or not? Is the Linux-PowerPC build a different implementation than the Windows-AMD64 build or not?

Even the Maxine Research VM, a meta-circular JVM written in Java and running inside of itself contains a tiny bit of platform-specific code.

If you want to, you can use the same PHP engine on different platforms and even with different webservers. For example, Zend Engine runs on Windows, Linux, macOS, several BSDs and probably a couple of other OSs, it runs on AMD64, PowerPC, ARM, and probably a couple of other CPUs, and it works with Apache, NginX, IIS, and probably a couple of other webservers.

  • It might also be worth noting that webserver and php are two seperate programs (ignoring mod_php and similar mods for a moment) which "talk" over CGI, FastCGI or any other protocol to eachother, nothing stops a apache or nginx server from using any PHP or even multiple at the same time if they wish so, given they implement a protocol the webserver understands. – tkausl Sep 20 '18 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.