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.
What does <? echo count ("123") ?> print out?
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.
Which of the following snippets prints a representation of
42 with two decimal places?
A. printf("%.2d\n", 42);
B. printf("%1.2f\n", 42);
C. printf("%1.2u\n", 42);
The answer is B. This question only requires that you remember the basic format strings for functions like printf() and sprintf().
$text = 'Content-Type: text/xml';
Which of the following prints 'text/xml'?
A. print substr($text, strchr($text, ':'));
B. print substr($text, strchr($text, ':') + 1);
C. print substr($text, strpos($text, ':') + 1);
D. print substr($text, strpos($text, ':') + 2);
E. print substr($text, 0, strchr($text, ':'));
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.
What is the value of $a?
$a = 123 == 0123;
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).
What is the value of $result in the following PHP code?
$int = $int * 2;
$int = 2;
$result = timesTwo($int);
The answer is null, because timesTwo() doesn't return anything.
The code below __________ because __________.
A. will work, class definitions can be split up into
multiple PHP blocks.
B. will not work, class definitions must be in a single PHP
C. will not work, class definitions must be in a single file
but can be in multiple PHP blocks.
D. will work, class definitions can be split up into
multiple files and multiple PHP blocks.
The answer is B, because class definitions must be in a single PHP block.
When turned on, __________ will __________ your script with
different variables from HTML forms and cookies.
A. show_errors, enable
B. show_errors, show
C. register_globals, enhance
D. register_globals, inject
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.
What will be the output of the following PHP code:
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.