I am a programmer and architect (the kind that writes code) with a focus on testing and open source; I maintain the PHPUnit_Selenium project. I believe programming is one of the hardest and most beautiful jobs in the world. Giorgio is a DZone MVB and is not an employee of DZone and has posted 636 posts at DZone. You can read more from them at their website. View Full User Profile

Java versus PHP

04.16.2010
| 38570 views |
  • submit to reddit
If you exclude C and its child C++, the most popular programming languages in the world are Java and PHP, which power most of the dynamic web. I have working experience with PHP and for academical purposes I am deepening my knowledge of Java, thus I'd like to point out similarities and key differences between these two languages. Every language has its pros and cons, so there's no absolute winner here.

History
Java was originally developed at Sun Microsystems in 1995, as part of the Java platform. Java applications are compiled to an intermediate bytecode which is run by a virtual machine. While originally intended for client software and in-browser applets (with the motto write once, run everywhere), Java is now a key infrastructure for many web applications.

PHP was born in the same year, and it was instead specifically created for the web, as a server-side scripting language to embed in html pages. It has evolved through 5 major versions to became one of the most popular web programming languages, thanks also to shared hosting services, which are particularly simple to set up for PHP applications and drive down their operational costs.

Typing
Java is built over static typing: variables must have a declared (possibly polymorphic) type. Thus, it is commonly judged as verbose, although the verbosity isn't necessarily linked to static typing.
PHP uses dynamic typing instead: variables assume the type of the value currently contained in them, and can change their type to satisfy implicit casts and conversions. This approach is prone to errors which would be detected by a compiler, but unit tests are a wonderful substitute for compile-time checks in dynamic languages.
Note that both languages have primitive types: Java because of the performance problems of wrapping integer and character variables in objects at the time of its creation in 1995, and PHP because it wasn't originally thought as an object-oriented language.

Object-oriented programming
PHP 5 borrowed its object-oriented paradigm from Java, which is the standard implementation of an object-oriented language today. After the release of PHP 5, a key point in the introduction of a serious object paradigm, PHP is evolving towards oop and has borrowed more from Java products: Doctrine 2 is an object-relational mapper inspired by Hibernate and JPA; phpDocumentor is built on the example of Javadoc; PHPUnit is one of the xUnit products, which derive from the original JUnit.

Execution model
PHP classes, functions and data structures, when they do not employ external infrastructure like caches or databases, are created in a script and they are garbage-collected at the end of the request. Java applications are instead kept in memory between requests, and the architecture of these two kinds of applications if fundamentally different - though, I would not say one model is superior to the other one. PHP pulls in execution only what it needs, and it pays back with the inability to run periodical tasks such as cron. Java applications can start multiple threads, but their management is much more complex, from the compiling phase to the deployment which includes servlets reloading.

Infrastructure and web
PHP is simple to deploy in its basic form (.php scripts), but this means that increasingly often the average developer has to use frameworks which builds standard infrastructure features over the simple PHP interpreter. Ironically, these framework are similar to Java ones; for example Zend Framework's controllers are the equivalent of servlets: classes with a standard constructor that extend a common base class and act on a request object to produce a response one.
Java has less native features built in the language, as it is not strictly oriented to the web, but it has them in frameworks which adhere to a standard, the servlet containers. PHP capabilities are hindered by the absence of standards.
For instance, the web.xml file in WAR packages, which represent a web application, define routes to map urls to servlets. Imagine if PHP had a standard for defining routes that Zend Framework, Symfony and their siblings respected: that seems science fiction.
References
Published at DZone with permission of Giorgio Sironi, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Manuel Jordan replied on Fri, 2010/04/16 - 12:51pm

I recall this type of comparison years ago, perhaps 5-6 when I was an University student

An engineer(my professor) simple said: "PHP is better than Java only because is faster"
Now he work with Java =)

Practically all my promotion friends chosen learn PHP, some of them no have idea about MVC and still feel the pain about spaghetti consequences, I am not an expert in PHP, I only know the basic things, but ORM world is applied in PHP?, if not, feel the absolutely pain when your boss tell you, do a migration to PostgreSQL now from MySql (enjoy the party!)

To end, the classic answer in such days in #java in the IRC such comparison is a heresy. 

Mike P(Okidoky) replied on Fri, 2010/04/16 - 3:02pm

* Java was released in '95, but developed before then * Many new comers don't realize that the JVM compiled bytecodes to machine code on the fly, resulting in similar performance than native C/C++ applications, in some cases better, in some cases worse. But that's a whole other debate. Point is bytecodes aren't interpreted, which is what many people that avoid Java think. * I thought php classes existed before 5. I have a few websites running using php 4, and I used classes, aka oop. * php does indeed look and feel like a one-shot mini-program for each web request. That also means that all the initialization code you need runs every time as well. You can use caching, but if you want to display live and up to date data you can't cache it, and all your startup code runs each time. This is one of the reasons that for anything non-trivial Java runs circles around php. You *can* place objects in a session of course, but then you have to start worrying about memory leaks, because now all of a sudden you're not in that one-shot realm any more. * Java frameworks makes the threading very simple and often completely transparent. You simply toss in some code in a servlet/jsp and you get something going, all nicely multi threaded under the hood. PHP can of course also handle multiple requests simultaneously by Apache creating a pool of sub-processes / threads. * Built in functions in php are really just library calls. Java has an insane amount of libraries to pick from. The API's you get out of the box in PHP are often just plain weird. Inconsistent. The PHP boys happily toss you around between PHP versions too. There are a ton of really weird fuzzy "features" like "magic quotes" and other convoluted bizarre awful mistakes. It's the most rogue awkward strange system I have every worked with. * The whole everything-is a reference, or not, is very confusing in PHP. Strange operators. PHP gets really bizarre and very awkward this way. On the upside, I have a bunch of code, a content management system, that I wrote years ago. I kept things as clean as I could. I created a way to query data in a nice way by putting the sql statements in an xml file with input and output parameters. I used the Smarty template engine which worked really nicely. I used custom tags to allow me to insert edit-features. I created this hierarchical node structure, with a base-node and a specialized node. A web request would then look up in the base node table the matching record (either by id or name), using the type field I would pull in the specialized record. Toss the results in a map (array in php), pass it to the matching Smarty template, et voila, you get a database driven web page. Here's the interesting part: after not having looked at it for a good number of years, I could go back in and fix bugs and even add a new feature in just a few hours. I didn't have to worry about upgrading JBoss, or horsing around with a overly convoluted complex framework. No restarting anything, reloading, staring at stacktraces (actually, I did have stack traces in php, which I displayed in a debug section of the webpage). End of day, there are definitely concepts and styles that PHP offers that the Java world can learn from. Generally, people in the Java world wouldn't recognize separating content from design from a hole in the ground. Graphic designers continue to be completely on the side lines, and hence, just about every Java based website looks like shit. But it's not just Java that fails to deliver there, it's people just not seeing the light on this front. That's also a whole other discussion I wouldn't mind having some time. I've created this Java server with some of these php concepts and actual effective separation of content and design way back when... Don't let anyone tell you that everything has been done once at least already. Look at the best of all worlds. Create a vision of what would be the ultimate web server, and then slap something together. You can embed Jetty in a standalone application and have a single servlet that handles every request. From it's just you and your code... no bulky awful frameworks that do a billion mystical fuzzy stuff that takes minutes to boot... Great thesis project !

Phil H. replied on Fri, 2010/04/16 - 5:10pm

I've blogged about something similar previously. http://pmcoder.wordpress.com/ IMO They both are good and often knocked by the other camp over things they don't really understand. The first complaint I always hear about PHP is spaghetti code which isn't true. Its a language like any other and you can organize your own code. There are also frameworks. I hear that Java is hard to use and/or slow and of course its not slow. And I could see some of the complaints about getting off the ground with Java EE but as of Java EE 6 I've has almost an easier time than I've had with PHP. I think the main drawback for PHP would be the wall you hit when you need big enterprise features. But as long as you won't need them you'll be fine especially with all the frameworks out there. The drawback for Java is that its hard to find hosting if you're doing small client projects. But I've been playing with Google App Engine and it looks like I've found my way around enough to handle needs from low end to high. In the end though I've decided to take the opportunity I currently have to move into the Java world. I could really use the benefits of Java app servers for what I'm doing now. Also it doesn't seem like the PHP world isn't evolving much at the moment and that worried me a bit.

Alosh Bennett replied on Tue, 2010/04/20 - 6:24am


Java and its related technologies would be my choice any day for building an enterprise app. But it cannot match the ease with which you could get your small web app running in PHP. It is a headache to get Apache to serve java requests. Most of the content management systems run on PHP.
The languages have evolved pretty much the way it has been used for.
On a side note, the look and feel of a site has got nothing to do with java or PHP. If you have a decent CSS, you've got a good looking site. It goes into PHP's favour that web designers are more comfortable with PHP.

Robert Enyedi replied on Wed, 2010/04/21 - 5:26pm

So here's the deal:

  • PHP is a mostly procedural programming language with a dynamic type system of arguable quality. It was designed for a CGI execution model and is only really suitable for a request-response style Web development.
  • Java is a language that sits on top of a bytecode execution platform (the JVM). It is a truly object-oriented general-purpose language. There are mature third party libraries that help the development all kinds of applications: desktop, mobile, Web. Regarding Web development, it can be done using the request-response approach or can be used for event-driven and component-based Web applications (using GWT, JSF, Eclipse RAP, e4). High levels of modularity, isolation and extensibility can be achieved even today using OSGi.

When planning ahead one's career, it is important to understand that Java and PHP overlap only on the request-response Web development part. When (and not if) the majority of Web development work will move to a component-based approach, PHP might easily become irrelevant. Java and the JVM certainly will not, because they are leading the transformation itself. The large businesses know this and it's only fair for everyone to know this.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.