Professional PHP

PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.
« goto in PHP
un-Friendster: fired for blogging »

Why PHP is easier to learn than Java

August 4th, 2004

I ran across a Novice java programmer’s mistakes and Common java programming errors page. This is a dubious exercise, but I am going to try and use these pages to compare the learning curve of Java and PHP.

This is inherently unfair, as these are lists of known learning problems in Java that might not exist in PHP. Please tell me, what are the learning hurdles of PHP that might not exist in Java?

First, the Java mistakes:

Naming the Class Differently from its File Name
This is an example of unwanted entanglement: the need to keep two items synchronous across a distance. No such filename entanglements in PHP.
Advantage: +1 PHP

Comparing Strings with ==
Strings aren’t objects in PHP so naive == works as expected, although weak typing can make the results of == more difficult to predict and having a === operator means more things to learn. (see PHP syntax exam)
Advantage: +.5 PHP

Forgetting to Initialize Object Arrays
PHP doesn’t require an array to be declared with a certain type, so this bit of confusion isn’t really possible.
Advantage: +1 PHP

Putting Several Public Classes in One File
Not a limitation in PHP. Naive version works as expected.
Advantage: +1 PHP

Shadowing an Attribute with a Local Variable
Attributes must be prefixed with $this->, so it isn’t possible to shadow an attribute with a local variable, although it is still possible to forget to add the $this->.
Advantage: +1 PHP

Forgetting to call a Superclass Constructors
PHP has this problem as well. Unfortunately, in PHP before __construct, there was an unfortunate naming entanglement between the class name, the constructor, and the superclass name.
Advantage: tie (+1 java vs. PHP 4)

Catching Exceptions Incorrectly
I might be getting this one wrong, but I think both languages have the same problem.
Advantage: tie

Returning void from an Accessor method
The type declaration for return value is unnecessary in PHP. Naive version works as expected.
Advantage: +1 PHP

Calling Instance Methods from main()
This is a variation on the simple hello world argument. The Java hello world makes more demands on the knowledge of the programmer.
Advantage: +1 PHP

Treating Strings as In/Out Parameters
Again, in PHP, strings aren’t objects and the naive version works if you pass by reference.
Advantage: +1 PHP

Declaring a Constructor as a Method
The special __construct name eliminates this confusion.
Advantage: +1 PHP (tie java vs. PHP 4)

Forgetting to Cast Object Data Types
Dynamic typing means no casting means no forgetting.
Advantage: +1 PHP

Extending Interfaces
Dynamic typing in PHP means that interfaces need not be declared and used as much as in Java, but I’ll still call this a tie.
Advantage: tie

Calling Superclass Methods and Forgetting to Use the Return Value
Advantage: tie

Forgetting to Add AWT Components
N/A

Neglecting Import Statements
Java and PHP both ship with large libraries. The built in PHP functions, however, are universally available. However, Woe to the beginner that has to re-compile PHP.
Advantage: +.5 PHP

Forgetting to Start Threads
No threads, no problem.
Advantage: +1 PHP

Using Deprecated java.io.DataInputStream readLine()
N/A

Assigning and passing double literals as floats
I’ll probably get this one wrong, but PHP has only doubles. Thus, no problem. (even if it did have multiple types, I think that dynamic typing would avoid this problem)
Advantage: +1 PHP

Second, the Common (Java) programming errors:

Not specifying the size of a new array.
Arrays grow and shrink in PHP. size specification unnecessary.
Advantage: +1 PHP

Not using correct array bounds.
foreach eliminates this problem. I understand Java has an equivalent now.
Advantage: tie

Doing arithmetic on an instance of a wrapper class.
No wrappers, no problem.
Advantage: +1 PHP

Adding a value of a primitive type to a collection (a set or list) or a map.
Dynamic typing, primitives welcome.
Advantage: +1 PHP

Not casting the value of type Object returned by list.get(i) or map.get(key) to the required type.
Not a problem in PHP. (covered above)
Advantage: +1 PHP

Using static components unnecessarily.
A little nod to the static main here.
Advantage: tie

Not reading the next line inside a loop
N/A

Creating a string tokenizer for a line before checking the line is present
N/A

Reading all input before processing it
N/A

Threading code
No threading, no problem.
Advantage: +1 PHP

Doing nontrivial computation in a class constructor
Advantage: tie

Not using common API methods
Advantage: tie

Not breaking out of a loop when required
Advantage: tie

Not breaking at the end of each case in a switch-statement
Fall through capability in a switch statement isn’t used and useful enough to justify requiring the extra break on each case in the normal course of events. When fall through is used, it is confusing. (see single point of entry argument in php goto) A pox on both houses.
Disadvantage: tie

Assigning constant values to boolean variables in if-statement
In a way, I find the naive version can be a little easier to understand.
Advantage: tie

Declaring variables globally, unnecessarily
Advantage: tie

Repeating code that should be in a method called repeatedly
Advantage: tie

Being too complicated
I’m tempted to knock java in general for this for things like EJB, but I won’t.
Advantage: tie

Combining computation and input/output in a single, complex method
Its easy to mix html output and php. I’ll give this one to Java.
Advantage: +1 Java

Filed Under

  • PHP

Related Posts

  • PHP Framework Consolidation?
  • Classpath Considered Harmful
  • PHP Book sales trends versus Java and Ruby
  • Why is PHP Code Considered Hard to Maintain?
  • Delphi for PHP
You can leave a response, or trackback from your own site.

14 Responses to “Why PHP is easier to learn than Java”

  1. Hans says:
    8/5/2004 at 6:07 am

    I like PHP, but a few of these +1 seem to be -1 to me:

    Multiple classes to a file is -1 from my POV when developing. (There are 3 +1 given to PHP for lack of file/class relationship constraints, which seems rather excessive.) In Java there is no question about which file to open to see a class. In PHP it could be anywhere.

    “Not casting the value of type Object returned by list.get(i) or map.get(key) to the required type.”
    This is no longer an issue in Java 1.5, right?

    Coherency of language: -1 for PHP. PHP has some internal stuff that’s in objects, but most in functions. Function naming, parameter orders, and return values exhibit little consistency. Needing to use PHP in OO environment can often reasult in creating wrapper classes for basic procedural API (e.g. DB abstraction, File IO abstraction). Frankly, a mess.

    Lack of java-style overloading. PHP -1. The new PHP5 object model introduces signature checking (yay!) but since PHP doesn’t allow overloading a great deal of flexibilty was lost in this move (boo!). No longer can you override a method & provide an incompatible signature. This mixture of strict OO + PHP’s traditional loose typing is really shooting PHP in the foot.

    “No threads, no problem.” PHP being unable to run (w/ stability) in a multi-threaded environment would seem to me to be a weakness.

    On the exception front Java has a +1. Even though both languages do support exceptions, Java has an advantage with checked exceptions. Java requires that methods which can throw an exception (e.g. by nature of calling other methods that throw exceptions & not using try/catch block) must declare the exceptions they throw. PHP has no such feature, forcing developers using a library to rely on phpdoc documention or to examine the source. Java also supports finally { } which is extremely useful for cleanup.

    -Hans

  2. Hans says:
    8/5/2004 at 6:13 am

    Oh, one more comment. The mention of ‘threading code’ in the java mistakes document is talking about threading program logic, not traditional process threads. Still a -1 for PHP that it is not threadsafe.

    -H

  3. Daniel Holmes says:
    8/5/2004 at 9:09 am

    Calling PHP not threadsafe really isn’t correct either. Core PHP is and has been threadsafe for quite some time. The problem is that any of the countless C libraries you MAY compile into php may not be. Additionally, in PHP’s share nothing model you will never make the mistake of using threads–hence shooting yourself in the foot when it is time to scale your app across multiple servers. I say, +1 to php.

    BTW – I say +100 to Jeff for posting this. Sure, it is admittedly biased…but great fun none the less. ;-)

  4. Declan says:
    8/8/2004 at 10:25 pm

    With respect to Hans, I think that the title really says it all: “Why PHP is easier to learn than Java”. Of course Java is more the programmer’s cup-of-tea (coffee?), but that’s because it’s designed to be.

    Great post!

  5. Lars Norstrum says:
    8/9/2004 at 7:18 am

    Very biased opinions in here, some valid some not so.

  6. CT says:
    8/9/2004 at 11:59 am

    Very unbiased opinions in here, some invalid some so.

  7. mark says:
    8/18/2004 at 8:49 am

    PHP’s core is not thread-safe. Try using PHP in a threaded environment such as Tomcat without any additional extensions nor libraries. It doesn’t work. It’s not thread-safe. echo() is not thread-safe.

  8. mark says:
    8/18/2004 at 10:28 am

    Closing database connections in a try/catch/finally block.
    PHP +3

  9. unbiased says:
    8/19/2004 at 4:27 am

    You are right. PHP is easire to learn. Because it is a script based language. And as any scripting language it better be easy to learn.
    Some of the disadvantages of java above are actually the advantages. Ans some of the above advantages of PHP are actually yhe reasons why PHP is still used primarily for front end programming and not for core business logic.

  10. nana says:
    12/17/2004 at 7:50 pm

    Bagus, cuba untuk buat yang terbaik!!!

  11. WTF says:
    3/23/2005 at 6:10 pm

    “No threading, no problem”? That’s the stupidest thing I’ve ever heard!

    I’ll write a language that *excels* at adding two numbers together. It’ll be better than ANYTHING PHP can do because “no web support, no problem”, “no variables, no problem”, “no syntax, no problem”!

  12. Anonymous says:
    7/7/2007 at 12:29 am

    What about “no programming, no problems” ;)

  13. Anonymous says:
    5/2/2008 at 1:41 pm

    I know the thread has been dead for a while, but…

    Despite PHP being a scripting language, I still found quite a bit of “steepness” in the learning curve:

    • require
    • include
    • require_once
    • include_once
    • Returning from a file or an included file?
    • Two completely different manuals on object-oriented programming (PHP 4 vs. PHP 5)!
    • etc.
    • global
    • magic methods
    • variable variables
    • array_search($needle, $haystack) vs. strpos($haystack, $needle)
    • etc.

    It doesn’t help when you have to debug/maintain someone else’s code base.

    I also think HTML output / logic (emphasis on “slash”) should have given Java more than one point. And forgetting to use $this to access instance methods/variables is still something I do quite alot… (should have been at most a tie).

    (I could go on a rant about PHP for a while, but I’ve mostly forgotten most of the head-and-heart-aches.)

  14. Ian says:
    2/23/2010 at 4:49 pm

    What about learning an entirely different webserver for java, horrors like Tomcat, and jars and wars and tons of xml, and that’s just to get a simple webpage going.
    Compared to what getting running with apache and php on Redhat in what, a couple of hours?

    Hmmmm

Leave a Reply

Click here to cancel reply.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

code: use [code=php][/code].

Comment Preview

    Subscribe Feed
    Share Subscribe to this blog…
    Share Bookmark or share this page…
  • About

    My name is Jeff Moore. I'm a PHP programmer living in San Francico and working for a startup.

    More about me…

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (17)
    • Open Source (14)
    • PHP (98)
    • Software Design (29)
    • Usability (14)
    • Web Design (20)
  • Recent Comments

    • Programming Language Trends via Google  19
      Craigslist pva, jessica, Scott [...]
    • Looking Towards the Cloud  35
      bentonville multiple listing, cosmetic dental, Sam Brodish [...]
    • PHP versus ASP  8
      Marhta Blight, Ravi, Ryan Brooks [...]
    • How to Transfer Mac OS X Application Data between Computers  59
      Website Migration, harry the computer support guy, Dotty Salvage [...]
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  157
      lehuuphuc, Robert Parthemer, Lingerie Intimate [...]
    • PHP Games  25
      jessica, Tennille Cranor at Chilli Plants, Lucas Ortell [...]
    • un-PEAR-ing  5
      jessica, Eugene Panin, Arnaud [...]
    • The Legality of Republishing RSS Feeds  23
      kevinxiao, Marissa Miscovich, Quick Student Loans [...]
    • Faster Page Loading  4
      jessica, angular cheilitis, Aaron Rosenfeld [...]
    • PDO versus MDB2  15
      jessica, kevinxiao, Gavin [...]
  • Recent Posts

    • ZendCon: Writing Maintainable PHP Code
    • Looking Towards the Cloud
    • Holiday Tech Support
    • Closures are coming to PHP
    • php | tek Wrapup
    • php | tek 2008
    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)
  • Site

    • Archives
    • Log in
  • Search