Tag: Certification

Zend PHP 5 Certification – Self Test

Today and from now on, on a regular basis, I am going to write about the ZCE Test.
http://www.zend.com/store/education/certification/self-test.php

I blog about it, because Zend has this great test online but doesn’t provide the answers. I know some of you will benefit greatly by me telling you the whys on every answer. But you should always take the test first!

Question 1
How can precisely one byte be read from a file, pointed by $fp?

A) fseek($fp, 1)
B) fgets($fp, 1)
C) fgetss($fp, 1)
D) fgetc($fp)
E) All of the above

The correct answer is fgetc($fp).
fgetcGets character from file pointer

But this definition on php.net is faulty. Beware fgetc() returns per byte as in the exam.  So if you use this function on a utf-8 formated file, you may encounter problems trying to display the character. As the ô character in the code below is split up over several lines.

Try the following code:

<?php
header('Content-Type: text/html; charset=utf-8');
$fp = fopen('somefile.txt', 'r');
if (!$fp) {
echo 'Could not open file somefile.txt';
}
while (false !== ($char = fgetc($fp))) {
echo "$char\n";
}
?>

and create in the same directory the following file: somefile.txt with the following content: “Bientôt!”

This will result in character encoding problems. Please do not forget to save your files in the utf-8 format. You have this ability in all good editors. If your editor does not support this, throw it away, set it on fire and forget about it. Check out Eclipse Zend, Zend IDE , phpED,  Komodo, …

Question 2
What object method specifies post-deserialization behavior for an object?

A) __sleep()
B) __wakeup()
C) __set_state()
D) __get()
E) __autoload()

The correct answer is __wakeup().

 The magic functions __sleep and __wakeup

__sleep
The standard PHP function serialize() checks if your class has a function with the magic name __sleep. If so, that function is being run prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn’t return anything then NULL is serialized and E_NOTICE is issued.

The intended use of __sleep is to commit pending data or perform similar cleanup tasks. Also, the function is useful if you have very large objects which need not be saved completely.

__wakeup
Conversely, unserialize() checks for the presence of a function with the magic name __wakeup. If present, this function can reconstruct any resources that object may have.

The intended use of __wakeup is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.

Question 3
Where does the session extension store the session data by default?

A) SQLite Database
B) MySQL Database
C) Shared Memory
D) File System
E) Session Server

The correct answer is the File system. PHP saves session data at the end of every request by default in a file in the system’s temporary directory. This is a great solution as it will serve for most websites. But as websites tend to grow and receive more traffic. With more traffic your server gets slow and at a certain point you will want to add more servers, so the work load can be divided between them. At this point you might consider storing session data in a database. Luckily PHP offers a solution to overide the default. session_set_save_handler().If you want more information regarding this issue check out devshed.

Question 4
Which of the following data types cannot be directly manipulated by the client?

A) Cookie Data
B) Session Data
C) Remote IP Address
D) User Agent

The correct answer is Session Data. Everything else can easily be forged. As this information is all stored client side. And Session data is stored server side by your PHP script.

Question 5
What is the difference between isset() and other is_*() functions (is_alpha(), is_number(), etc.)?

A) isset() is a function call and is_*() are not function calls
B) is_*() are language constructs and isset() is not a language construct
C) isset() is a language construct and is_*() are not language constructs
D) is_*() return a value whereas isset() does not

Isset() is a language construct. These are built-into the language and, therefore, they behaves differently than functions. They have their own special rules. Check out the following code.

<?php
/* Variable functions, PHP supports the concept of variable functions. */
$fruit = array('banaan', 'apple');
$variableFunction = 'is_array';
var_dump($variableFunction($fruit));

/* But Variable functions won't work with language constructs such as echo(), print(), unset(), isset(), empty(), include(), require() and the like.*/
$variableFunction = 'isset';
var_dump($variableFunction($result));
?>

This code will result in a fatal error.

Question 6
What will be the value of $b after running the following code?

<?php
$a = array(‘c’, ‘b’, ‘a’);
$b = (array) $a;

A) TRUE
B) array(‘c’, ‘b’, ‘a’)
C) array(array(‘c’, ‘b’, ‘a’))
D) None of the above

The correct answer is array(‘c’,’b’,’a’) as (array) is a type conversion operator also known as a cast operator.

The casting operators allow you to cast variables to new types. Unlike the settype function, they return a value that is the value of the variable cast in the new type.
It is used simply as the data type you want to convert to enclosed in brackets and placed before an expression.

The casting operators are:

  • (int) or (integer) to cast to an integer
  • (float) or (real) to cast to a floating-point number
  • (string) to cast to a string
  • (bool) or (boolean) to cast to a boolean
  • (array) to cast to an array, in this case it is already an array, so no changes are being made to the array
  • (object) to cast to an object

Question 7
Which of the following function signatures is correct if you want to have classes automatically loaded?

A) function autoload($class_name)
B) function __autoload($class_name, $file)
C) function __autoload($class_name)
D) function _autoload($class_name)
E) function autoload($class_name, $file)

The correct answer is __autoload($class_name).

Question 8
What is the best way to run PHP 4 and PHP 5 side-by-side on the same Apache server?

A) Run one as an Apache module, the other as a CGI binary.
B) Run both as a CGI binary.
C) Just use .php4 for PHP 4, and .php for PHP 5.
D) Use .php for both but use different document roots.

You should run one as an Apache module, the other as a CGI binary.

Of course PHP as a CGI is not quite the same as an Apache module, performance / $_SERVER variable-wise.  You’ll get better performance with PHP setup as a module. Not only will you not have to deal with the CGI performance hit,  but if some of your scripts use basic http authentication, they do not work if you run PHP as a CGI. Naturaly there are ways around this, but that is what Google is for.

I hope you learned some stuff, and I’ll see you at the next Zend Certified Engineer 8 questions test.

Sunny greetings from Belgium
Nick Belhomme

Ps. Do not forget to bookmark: http://www.zend.com/store/education/certification/self-test.php and take the test as it will keep you sharp or prepare you to take the real ZCE  exam!