Java or PHP

By

Version

Date

Abdul Habra

0.1

August 8, 2002

1 Introduction

This document compares PHP (4) and Java (1.4) features. Please note that the author has more experience with Java than with PHP. All the PHP information were obtained from www.php.net. (Added on 2004.06.08: Later versions of PHP/Java/JSP may have other features that you should consider)

If you have any corrections or suggestions, please let the author know.

2 PHP Tutorial

Using the PHP tutorial available at: http://www.php.net/manual/en/tutorial.php

The following table compares all the PHP features presented in the tutorial with their counterparts in Java

3 Language Features

This section compares language features of both languages. Some features that are similar are not presented

4 Conclusion

The following shows the scores for each language:

PHP is suitable for small web based applications. Notice how PHP won in features that are geared towards the scriptís writer, e.g. Variable variable name, or multi-line strings.

Java is more general and is suitable for larger applications. Notice how Java won in Object Oriented features, code packaging, multi-threading, and security.

Notes

1. Added on 2004.07.21: Passing by reference in Java: I have received many objections to my statement that Java does not support passing parameters by reference. I still think this statement is accurate. Please see:

The Java Programming Language, 2nd ed. by Ken Arnold and James Gosling. ISBN 0-201-31006-6. Section 2.6.1 Parameter Values. Page 40. Last two lines of 3rd paragraph clearly states:

"There is exactly one parameter passing mode in Java -pass by value- and that helps keep things simple."

Please read the whole section for a good explanation. Next is an example that demonstrates the issue:

class Person { String name; } ... void method1(Person p) { p.name= "Dirk"; // this is ok } void method2(Person p) { p= new Person(); p.name= "John"; // only the methodís local p will change } void method3(final Person p) { p= new Person(); // will generate a compilation error. p.name= "Phil"; } void test() { Person p= new Person(); p.name= "abdul"; method1(p); // p.name is Dirk now. method2(p); // p.name is still Dirk, not John }