| By: | Abdul Habra |
| Email: | ahabra@yahoo.com |
| Version: | 0.1 |
| Date: | August 8, 2002 |
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.
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 | |
|---|---|---|
| 1 | PHP | <?php echo "Hello World<p>"; ?> |
| Java | <% out.print("Hello World<p>"); %> | |
| Winner | Tie. | |
| 2 | PHP | Echo "This spans multiple lines. The newlines will be output as well"; |
| Java | Not supported. Java does not support strings that span multiple lines in the source code. | |
| Winner | PHP. | |
| 3 | PHP | <?php echo $_SERVER["HTTP_USER_AGENT"];?> |
| Java | <%=request.getHeader("User-Agent"); %> | |
| Winner | Java. Java abstracts the request/response into well-organized classes. PHP supports also <?=expression ?> syntax. | |
| 4 | PHP | <?php phpinfo(); ?> |
| Java | Java does not have global variables. | |
| Winner | Java. Not having global variables helps in maintenance and upgrade of software. | |
| 5 | PHP | <?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/>"); } %> |
|
| Winner | Tie. With Java preferred because string operations are encapsulated in the String class. | |
| 6 | PHP | <?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> <% } %> |
|
| Winner | Tie. | |
| 7 | PHP | <?php echo $_POST["name"]; ?> |
| Java | <% out.print(request.getParameter("name")); %> | |
| Winner | Java. Java abstracts the request/response into well-organized classes. | |
This section compares language features of both languages. Some features that are similar are not presented
| # |
Features Compared |
|
|---|---|---|
| 1 | Feature | Data types |
| PHP | boolean, integer, float, string, array, object. | |
| Java | boolean, char, byte, short, int, long, float, double, String, array, Object. | |
| Winner | Java. | |
| 2 | Feature | Variables names |
| PHP | Variables are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. |
|
| Java | There is no special character to start the variable name. The variable name is case-sensitive. | |
| Winner | Java. | |
| 3 | Feature | Variable declaration |
| PHP | The 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. | |
| Java | Variables must be declared with a specific data type before usage. | |
| Winner | Java. 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. | |
| 4 | Feature | Global variables |
| PHP | PHP has a large number of predefined variables. | |
| Java | Java does not have global variables. | |
| Winner | Java. Global variables introduce possible bugs in large software. | |
| 5 | Feature | Variable variable names. A variable that contains the name of another variable. |
| PHP | Supported. | |
| Java | Not supported. | |
| Winner | PHP. | |
| 6 | Feature | Constant declaration |
| PHP | Constants are defined through a function, e.g.: define("MYCONST", "Hello world"); Defines a constant named MYCONST. |
|
| Java | Constants are declared like variables with a “static final” modifier,
e.g: static final String MYCONST = "Hello world"; |
|
| Winner | Java. Introducing a special function to define constants is counter-intuitive. | |
| 7 | Feature | Using libraries |
| PHP | PHP includes libraries. | |
| Java | Java import libraries. | |
| Winner | Java. Including libraries can introduce variable scope issues. Packages are better structured than included libraries. | |
| 8 | Feature | Method overloading |
| PHP | Not supported. | |
| Java | Supported. | |
| Winner | Java. | |
| 9 | Feature | Passing method parameters |
| PHP | Passed by value and by reference. | |
| Java | By value only. (Added on 2004.07.21: Please See Note 1) | |
| Winner | PHP. | |
| 10 | Feature | Varying number of method’s parameters |
| PHP | Supported. | |
| Java | Not supported, but can be done with method overloading. | |
| Winner | PHP. | |
| 11 | Feature | Variable functions: A variable name contains the name a function to call. |
| PHP | Supported. | |
| Java | Supported with reflection. | |
| Winner | PHP. Java is harder to do. | |
| 12 | Feature | Invoking class members |
| PHP | object->member() | |
| Java | object.memebr() | |
| Winner | Tie. | |
| 13 | Feature | Multiple inheritance |
| PHP | Not supported. | |
| Java | Not supported. | |
| Winner | Tie. | |
| 14 | Feature | Static methods |
| PHP | ClassName::method() | |
| Java | Method must be declared static then called as
follows: ClassName.method() |
|
| Winner | Tie. | |
| 15 | Feature | Object serialization |
| PHP | Supported. Serializes to a stream. | |
| Java | Supported. Serializes to a stream or to XML. | |
| Winner | Java. | |
| 16 | Feature | Interfaces |
| PHP | Not supported. | |
| Java | Supported. | |
| Winner | Java. | |
| 17 | Feature | Scope of class members |
| PHP | Only default public scope. | |
| Java | public, private, protected, and default (package). | |
| Winner | Java. | |
| 18 | Feature | Polymorphism |
| PHP | Not supported. Methods cannot be overridden. | |
| Java | Supported. | |
| Winner | Java. | |
| 19 | Feature | Abstract classes and methods |
| PHP | Not supported. | |
| Java | Supported. | |
| Winner | Java. | |
| 20 | Feature | Exception handling |
| PHP | Errors can be ignored or raised. | |
| Java | Structured handling with try/catch/finally construct. | |
| Winner | Java. | |
| 21 | Feature | Multiple threads processing |
| PHP | Not supported. (Supports Unix style process control with an add-on library that works on Unix only). | |
| Java | Supported. | |
| Winner | Java. | |
| 22 | Feature | Components |
| PHP | Not supported. | |
| Java | Supported with Java Beans and EJBs. | |
| Winner | Java. | |
| 23 | Feature | Security |
| PHP | Limited support with the “safe mode” feature. | |
| Java | Detailed support for applets, servlets, EJBs, and applications. | |
| Winner | Java. | |
The following shows the scores for each language:
| Number of features | 30 |
| PHP Won | 5 |
| Java Won | 19 |
| Tie | 6 |
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) {
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
}
| Page Last Updated 2004.07.21 |
|