Zend Certification Self Test

11 Apr 2005

In Preparing for the Zend Certification, I provided the answers (with explanations) to the Zend PHP Certification Self Test. Zend has since updated the self test, so I'm again providing the answers to it. Of course, I recommend that you take it before you read this - this is for those who don't understand a particular answer or can't figure out which one(s) they missed.

Question 1

  1.  
  2. A. 3
  3. B. False
  4. C. Null
  5. D. 1

The answer is D (1), because only a single argument is given to count(). Incarnations of this question typically confuse people due to the fact that count() is almost always used to count elements in an array, so they're not familiar with using it in any other context. Aside from that, it's pretty straightforward.

Question 2

  1. 42 with two decimal places?
  2.  
  3. A. printf("%.2d\n", 42);
  4. B. printf("%1.2f\n", 42);

The answer is B. This question only requires that you remember the basic format strings for functions like printf() and sprintf().

Question 3

  1. $text = 'Content-Type: text/xml';
  2. Which of the following prints 'text/xml'?
  3.  
  4. A. print substr($text, strchr($text, ':'));
  5. B. print substr($text, strchr($text, ':') + 1);
  6. C. print substr($text, strpos($text, ':') + 1);
  7. D. print substr($text, strpos($text, ':') + 2);

The answer is D. The string position of : (the colon) is two less than the string position of the t, so strpos($text, ':') + 2 provides the string position of the t. The substr() syntax therefore says that we want the substring of $text that begins with the t and continues to the end of the string.

Question 4

  1. <?php
  2.     $a = 123 == 0123;
  3. ?>
  4.  
  5. A. True

The answer is B (False). This question is challenging, because you must know that PHP treats 0123 as the octal representation of 83 due to the leading zero (64 + 16 + 3 = 83).

Question 5

  1.  
  2. <?php
  3.  
  4.     function timesTwo($int)
  5.     {
  6.         $int = $int * 2;    
  7.     }
  8.  
  9.     $int = 2;
  10.  
  11.     $result = timesTwo($int);
  12.  

The answer is null, because timesTwo() doesn't return anything.

Question 6

  1.  
  2. <?php
  3.  
  4.     class Foo
  5.     {
  6.  
  7. ?>
  8.  
  9. <?php
  10.  
  11.         function bar()
  12.         {
  13.             print "bar";
  14.         }
  15.  
  16.     }
  17.  
  18. ?>
  19.  
  20. A. will work, class definitions can be split up into
  21.    multiple PHP blocks.
  22. B. will not work, class definitions must be in a single PHP
  23.    block.
  24. C. will not work, class definitions must be in a single file
  25.    but can be in multiple PHP blocks.
  26. D. will work, class definitions can be split up into

The answer is B, because class definitions must be in a single PHP block.

Question 7

  1. different variables from HTML forms and cookies.
  2.  
  3. A. show_errors, enable
  4. B. show_errors, show
  5. C. register_globals, enhance

The answer is D. While the first two answers make no sense, both C and D seem viable, and the difference between the two seems entirely subjective. If you consider register_globals to have a negative connotation, you're more likely to think of its behavior as injecting rather than enhancing. However, I'm not sure that a distaste of register_globals is a necessary characteristic of an expert PHP developer.

Question 8

  1.  
  2. <?php
  3.  
  4.     echo count(strlen("http://php.net"));
  5.  

The answer is 1. This is almost the same question as the first one, so the only challenge is understanding what count() does when given something other than an array.