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 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

#  Features Compared
1PHP<?php echo "Hello World<p>"; ?>
Java<% out.print("Hello World<p>"); %>
WinnerTie.
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.
WinnerPHP.
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.
5PHP<?php 
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) { 
  echo "You are using Internet Explorer<br/>";
} ?>
Java<%
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>
<% } %>
WinnerTie.
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.
WinnerJava.
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.
WinnerJava.
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.
PHPSupported.
JavaNot supported.
WinnerPHP.
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.
JavaSupported.
WinnerJava.
9FeaturePassing method parameters
PHPPassed by value and by reference.
JavaBy value only. (Added on 2004.07.21: Please See Note 1)
WinnerPHP.
10FeatureVarying number of methodís parameters
PHPSupported.
JavaNot supported, but can be done with method overloading.
WinnerPHP.
11FeatureVariable functions: A variable name contains the name a function to call.
PHPSupported.
JavaSupported with reflection.
WinnerPHP. Java is harder to do.
12FeatureInvoking class members
PHPobject->member()
Javaobject.memebr()
WinnerTie.
13FeatureMultiple inheritance
PHPNot supported.
JavaNot supported.
WinnerTie.
14FeatureStatic methods
PHPClassName::method()
JavaMethod must be declared static then called as follows: 
ClassName.method()
WinnerTie.
15FeatureObject serialization
PHPSupported. Serializes to a stream.
JavaSupported. Serializes to a stream or to XML.
WinnerJava.
16FeatureInterfaces
PHPNot supported.
JavaSupported.
WinnerJava.
17FeatureScope of class members
PHPOnly default public scope.
Javapublic, private, protected, and default (package).
WinnerJava.
18FeaturePolymorphism
PHPNot supported. Methods cannot be overridden.
JavaSupported.
WinnerJava.
19FeatureAbstract classes and methods
PHPNot supported.
JavaSupported.
WinnerJava.
20FeatureException handling
PHPErrors can be ignored or raised.
JavaStructured handling with try/catch/finally construct.
WinnerJava.
21FeatureMultiple threads processing
PHPNot supported. (Supports Unix style process control with an add-on library that works on Unix only).
JavaSupported.
WinnerJava.
22FeatureComponents
PHPNot supported.
JavaSupported with Java Beans and EJBs.
WinnerJava.
23FeatureSecurity
PHPLimited support with the "safe mode" feature.
JavaDetailed support for applets, servlets, EJBs, and applications.
WinnerJava.
4 Conclusion

The following shows the scores for each language:

Number of features 30
PHP Won5
Java Won19
Tie6

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
}


Comments