jay johnston dot com
mootools php faith camera linux

php

Possible PHP Parser Error



Perplexing PHP Parser Ponderance

Is this an error in the parser or just a feature of the language?



  
// what will this print out in php5?

  
$earth 'World';
  
$string1 "Hello ".
  
$string2 $earth.'!';  
  
$string $string1.$string2;
  echo 
$string."n";

  
// did you catch it, or did you run it to find out?

  // here it is again in slow motion:

  // note that this fooling of the parsing error
  // is quickly given away by showing E_NOTICE..
  
error_reporting(E_ALL);

  
// begin with a variable
  
$earth 'World';

  
// now create the first line of
  // multi-line concatenation
  
$string1 "Hello ".

  
// now forget that we're concatenating
  
$string2 $earth.'!';  // $string2 is now equal to World!

  // now try to put together
  // what you meant to concatenate earlier
  
$string $string1.$string2;

  echo 
'string1: '.$string1."n"//string1: Hello World!
  
echo 'string2: '.$string2."n"//string2: World!
  
echo 'string: '.$string."n";   //string: Hello World!World!

  // is this an error in the parser?
  // or is this a feature-not-bug?



Last Updated: 2009-10-27 21:37:03

Creative Commons License Valid XHTML 1.0 Transitional Powered by PHP
Jay Johnston is a Zend Certified Engineer in PHP 5