Documents‎ > ‎Java Related‎ > ‎

Java or PHP

ByAbdul Habra
Version 0.1
DateAugust 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 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:

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

#  Features Compared
1PHP<?php echo "Hello World<p>"; ?>
Java<% out.print("Hello World<p>"); %>
2PHPEcho "This spans
multiple lines. The newlines will be 
output as well";
JavaNot supported. Java does not support strings that span multiple lines in the source code.
3PHP<?php echo $_SERVER["HTTP_USER_AGENT"];?>
Java<%=request.getHeader("User-Agent"); %>
WinnerJava. Java abstracts the request/response into well-organized classes. PHP supports also <?=expression ?>syntax.
4PHP<?php phpinfo(); ?>
JavaJava does not have global variables.
WinnerJava. Not having global variables helps in maintenance and upgrade of software.
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) { 
  echo "You are using Internet Explorer<br/>";
} ?>
if (request.getHeader("User-Agent").indexOf("MSIE")>-1) { 
  out.print("You are using Internet Explorer<br/>");
} %>
WinnerTie. With Java preferred because string operations are encapsulated in the String class.
6PHP<?php if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) { ?>
  <h3>strstr must have returned true</h3>
  <center><b>You are using Internet Explorer</b></center>
<?php } else { ?>
  <h3>strstr must have returned false</h3>
  <center><b>You are not using Internet Explorer</b></center>
<?php } ?>
Java<% if (request.getHeader("User-Agent").indexOf("MSIE")>=0) { %>
  <h3>indexOf must have returned >=0 </h3>
  <center><b>You are using Internet Explorer</b></center>
<% } else { %>
  <h3>indexOf must have returned -1</h3>
  <center><b>You are not using Internet Explorer</b></center>
<% } %>
7PHP<?php echo $_POST["name"]; ?>
Java<% out.print(request.getParameter("name")); %>
WinnerJava. Java abstracts the request/response into well-organized classes.
3 Language Features

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

#Features Compared
1FeatureData types
PHPboolean, integer, float, string, array, object.
Javaboolean, char, byte, short, int, long, float, double, String, array, Object.
2FeatureVariables names
PHPVariables are represented by a dollar sign followed by the name of the variable. 
The variable name is case-sensitive.
JavaThere is no special character to start the variable name. The variable name is case-sensitive.
3FeatureVariable declaration
PHPThe variable is declared when it is created. Its type is implied from the assigned value. A variable can change its type if it is assigned a new value.
JavaVariables must be declared  with a specific data type before usage.
WinnerJava. It is convenient for a small program not to require variable declarations, but for large software, this is harmful. Variables changing their types based on their value are very dangerous in large programs.
4FeatureGlobal variables
PHPPHP has a large number of predefined variables.
JavaJava does not have global variables.
WinnerJava. Global variables introduce possible bugs in large software.
5FeatureVariable variable names. A variable that contains the name of another variable.
JavaNot supported.
6FeatureConstant declaration
PHPConstants are defined through a function, e.g.:
  define("MYCONST", "Hello world");
Defines a constant named MYCONST.
JavaConstants are declared like variables with a "static final" modifier, e.g:
static final String MYCONST = "Hello world"; 
WinnerJava. Introducing a special function to define constants is counter-intuitive.
7FeatureUsing libraries
PHPPHP includes libraries.
JavaJava import libraries.
WinnerJava. Including libraries can introduce variable scope issues. Packages are better structured than included libraries.
8FeatureMethod overloading
PHPNot supported.
9FeaturePassing method parameters
PHPPassed by value and by reference.
JavaBy value only. (Added on 2004.07.21: Please See Note 1)
10FeatureVarying number of methodís parameters
JavaNot supported, but can be done with method overloading.
11FeatureVariable functions: A variable name contains the name a function to call.
JavaSupported with reflection.
WinnerPHP. Java is harder to do.
12FeatureInvoking class members
13FeatureMultiple inheritance
PHPNot supported.
JavaNot supported.
14FeatureStatic methods
JavaMethod must be declared static then called as follows: 
15FeatureObject serialization
PHPSupported. Serializes to a stream.
JavaSupported. Serializes to a stream or to XML.
PHPNot supported.
17FeatureScope of class members
PHPOnly default public scope.
Javapublic, private, protected, and default (package).
PHPNot supported. Methods cannot be overridden.
19FeatureAbstract classes and methods
PHPNot supported.
20FeatureException handling
PHPErrors can be ignored or raised.
JavaStructured handling with try/catch/finally construct.
21FeatureMultiple threads processing
PHPNot supported. (Supports Unix style process control with an add-on library that works on Unix only).
PHPNot supported.
JavaSupported with Java Beans and EJBs.
PHPLimited support with the "safe mode" feature.
JavaDetailed support for applets, servlets, EJBs, and applications.
4 Conclusion

The following shows the scores for each language:

Number of features 30
PHP Won5
Java Won19

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.


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) { "Dirk";       // this is ok

void method2(Person p) {
  p= new Person(); "John";      // only the methodís local p will change

void method3(final Person p) {
  p= new Person();     // will generate a compilation error. "Phil";

void test() {
  Person p= new Person(); "abdul";
  // is Dirk now.

  // is still Dirk, not John