Monday, September 10, 2007

Are you a PHP 5 guru?

If you are able to answer the following questions, you are certainly one of them! Consider the following script:
<?php
error_reporting
(E_ALL | E_STRICT);
$test[isset($test)] = $test;
var_dump($test);
?>

  1. How many notices are displayed?
  2. What is the output?
Ok, you replied with:
  1. 1 notice (Notice: Undefined variable: test in ...)
  2. array(1) {
    [0]=>
    NULL
    }
Congratulations! You just failed your Zend Engineer Certification.
Joke aside, correct answers were:
  1. 0 notice
  2. array(1) {
    [0]=>
    array(1) {
    [0]=>
    *RECURSION*
    }
    }
Well, in PHP 4 you would be in the right, but PHP 5 introduces a new feature that let you construct a kind of recursive array... More interesting is that if you used an isset on the right part of the assignment it would return you false!

1 comment:

Jan! said...

Is this still accurate? This is what I get:

Notice: Undefined variable: test in E:\Projects\back-orifice\site.20090223\jmoe\- on line 3
array(1) {
[0]=>
NULL
}

My PHP:
PHP 5.2.6 (cli) (built: May 2 2008 18:02:07)