And now instead, 5 things Java envies PHP for

DZone 's Guide to

And now instead, 5 things Java envies PHP for

· Agile Zone ·
Free Resource

Disclaimer: I do not want to start a religion war on Java vs. PHP. You know the old saying that we should learn a new programming language every now and then? The purpose is to get a fresh perspective and collect new ideas that we can use in our preferred platform. Every language can teach us something new, even if Java is already the standard for programmers education.

The comparison is only on web applications and dynamic websites, as PHP does not compete on the desktop. I am a PHP programmer, but I work in Java on university-related projects. If you notice something is missing from the comparisons in this list, feel free to comment and complete my depiction of Java features, and of PHP too.

Let's start.

You can't produce memory leaks

A shared nothing environment can sometimes be an hindrance, but it's definitely helping for performances and robustness. Everything in PHP is closed and destroyed at the end of the script: even if a library or resource you cannot avoid using leaks memory like there's no tomorrow, that memory will be freed at the end of the script. There is also a memory_limit directive, by the way, in case the script is doing something heavy like database population.

Even in the case of infinite recursion, a segmentation fault happen in the process currently running, which crashes. The next request can be served seamlessly, as if nothing has happened. No application state means it can't be left in an awkward state, and as some thoughtful commenters state in the comments to the previous article, no need for locks and synchronization. Multithreading in PHP is simply the use of multiple scripts (asynchronicity must be managed out of the script.)

No compilation required

I know, IDEs take care of everything and when you hit any keystroke, they simply recompile everything is out of date. However, in PHP there is no compilation step required by the programmer, and for example you run tests on the included source files. Libraries are shipped as source code and when they fail it's a matter of seconds to go to the particular line which generates the error.

The absence of explicit compilation does not mean that there are no compile-time checks: every script is compiled Just In Time before execution, and the opcodes (equivalent of bytecode) can be cached in production environments. Thus, a script with syntax errors cannot be executed at all; a class extending a not existing parent class cannot be defined. And so on. Without explicit compilation, there is one step less in testing, CI, and deployment (unless you are Facebook and invent HipHop). As far as I know, Java JIT compilers go from bytecode to native code, but they do not start from source code.

Unified platform

Choices are usually good for developers, but alternate implementations shouldn't be named with the same package and class name.

For example, when I was working with OSGi, I found out a show stopper issue was caused by the usage of a org.w3c.dom package which was different from the expected one, since Xerces wanted to use its own org.w3c.dom.Document, different from the one included with the JVM.

I have never seen duplicate class names in PHP code, even before namespaces were introduced. Maybe the scale of object-oriented code in PHP is not so high to produce such conflicts, but when you instance a class you know exactly what code you are referring to; and its source code is always there for inspection when an abstraction fails.

Database (and more) batteries included

Every supported relational database driver is usually enabled, and shipped with the binary PHP package. You can disable loading of the extensions you do not use, but enabling MySQL or Postgres for PDO is usually a matter of uncommenting a line in php.ini. No Jars to download or to pick with Maven during the build, no multiple versions incompatibilities.

And it's not only a matter of talking to a database. Just try reading a file:

File file = new File("C:\\MyFile.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;

try {
fis = new FileInputStream(file);

bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

while (dis.available() != 0) {


} catch (FileNotFoundException e) {
} catch (IOException e) {

compare with

<?php echo file_get_contents('MyFile.txt'); ?>

I know there's Apache Commons, but do I have to download and link an external library just to read a file?

I know also performance can be affected with the PHP solution, but we have fread() and similar functions if you really want to read in blocks.

Let's try with an XML file:

try {
File file = new File("c:\\MyXMLFile.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
NodeList nodeLst = doc.getElementsByTagName("employee");

for (int s = 0; s < nodeLst.getLength(); s++) {

Node fstNode = nodeLst.item(s);

if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
System.out.println("First Name : " + ((Node) fstNm.item(0)).getNodeValue());

} catch (Exception e) {

compare with:

$xml = simplexml_load_file('MyXMLFile.xml');
foreach ($xml->employee as $e) {
echo "First name: ", $e->firstname, "\n";

Again, built in the language. Let's skip JSON...

Shared hosting

Shared hosting is a bittersweet characteristic of PHP. It's cheap: in Italy you can get an host with 4 MySQL databases for less than 10 Euros, and also for free if you accept a subdomain (prices go up with required performances, technical support, and services like SSH). The money mostly covers the domain name registration. The same machine can support many websites, as nothing is in execution until a user requests a page.

It's easy to deploy to: it only needs a phpMyAdmin instance, to manage the database via browser, and some file transfer mechanism (back in the old days, FTP, but also svn or git if there is shell access). Zero configuration external to your own application and database: once you have a build, you can just drop it in the right folder, or change a symlink.

The barrier to entry is very low, which means also the average PHP programmer won't have read all of Dijkstra papers. PHP is often treated as a toy language due to the results obtained by the low-end segment of programmers, but shared hosting is one of the key points of PHP's popularity.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}