System.out.print in PHP

March 9th, 2007 by Ivo

We are recruiting people on a regular basis, and since it's sometimes difficult to find people with both a computer science background and PHP experience, we also recruit people that are fluent in other languages. Teaching a c++/java/delphi-guy to do PHP in general is easier than teaching a PHP hobbyist good engineering practices.

So yesterday I was reviewing a code sample that a job applicant had written during a test and I encountered the following line of code:

 
System.out.print($result);
 

It's not hard to guess what his background was :) , but I was initially stunned that this actually works in php.

My first thought was that this was because I installed the Java Bridge from Zend Platform, but the bridging API does not allow the direct use of java objects like this. So was there some evil hidden hack in PHP?

Of course not; eventually the truth was uncovered by the notices in the errorlog:

PHP Notice: Use of undefined constant System - assumed 'System'

Ah, that makes sense. It just sees them as constants which do not exist and are interpreted as strings, and apparently, this is a valid PHP statement as well:

 
  "this"."is"."a"."string";
 

I cannot find any practical use for being able to write a statement without assignment or output like that, but it does enable code beautification:

 
  I.need.to.get.some.sleep(1);
 

Of course I would have to strongly advise against this but it was fun to notice that this works. :-)

P.S. shameless plug: this week we launched the first preview of our new web 2.0 pet project: http://www.mockatoo.com; not much to see just yet, but consider it a preview of what we are up to.

10 Responses to “System.out.print in PHP”

  1. March 09, 2007 at 9:39 am, John Herren said:

    Early nomination for best PHP post of 2007.

  2. March 09, 2007 at 12:24 pm, Manfred Weber said:

    Very funny! :)

  3. March 09, 2007 at 4:25 pm, Alexey Zakhlestin said:

    Awesome! You made my day :)

  4. March 09, 2007 at 4:50 pm, Jeff Moore said:

    I’ll second that nomination. Thanks for for the smile.

  5. March 09, 2007 at 5:46 pm, Sara Golemon said:

    zomg….

    That’s going to be my new bonus-points interview question…. “Why does this bit of code actually *work* in PHP?”

  6. March 09, 2007 at 6:10 pm, schmalls said:

    You could also define the constants ‘System’ and ‘out’ and then it will probably get through without any E_STRICT errors. I didn’t check it myself though.

  7. March 09, 2007 at 9:15 pm, alf said:

    Perl is even more fun:

    $x = pp; $y = qq; $z = rr;

    Now, $x is ‘pp’, $y is ‘ = rr’, $z is undefined

  8. March 09, 2007 at 10:17 pm, John said:

    Ha! This makes me smile, just goes to show that PHP is one of the most forgiving languages around, it even makes room for confused Java programmers ;-)

    My company is currently looking to recruit programmers with Java and PHP experience, but realistically if they come from a “traditional” CS background they only have Java, especially recent grads, so we have to settle for someone who is open minded enough to want to learn something different on the job then what they were taught in college.

    Shame that more CS courses don’t teach dynamic languages like PHP, Perl and Ruby…

  9. March 10, 2007 at 8:59 pm, Gaylord Aulke said:

    Hahahaha! Very cool story. Exactly the same story for us with php-hobbyists and java/C people. Very good code snippet you showed.

    I think this should go in the next PHP certification exam: “What will this code output” ;-)

  10. April 11, 2007 at 9:36 pm, kucerar said:

    I’ll help you, after this is my framework too ;-) …ask them what is the first step in solving a problem?

    Answer backwards below.

    melborp eht enifed