Mcloide's resources library

All about PHP, Javascript, concepts and development

Posts Tagged ‘zend certification’

Zend Exam Tips – double check the question before answering

Posted by mcloide on September 10, 2008

http://knowfree.net/

If you are a professional and you are working full time (sometimes even more), like yours trully, then here comes a good tip.

Not only once, but more often than I would like, I have answered wrongly a question just because I have read it fast.

Take a look at this question:

What is the output of the following?

<?php
function 1dotEach($n)
{
if ($n > 0)
{
1dotEach(–$n);
echo “.”;
}else
{
return $n;
}
}
1dotEach(4);
?>

Answer…
A) …0
B) Parse Error: Syntax Error
C) …..
D) ….
E) …

At first you would consider A or E as the answer for this question, but, take a second look at the line where the function is defined. The function starts with 1, a number and in PHP you cant have functions names,  variables names, class names, object names, etc,  starting with numbers which make B the correct answer for this.

Posted in PHP | Tagged: , , , , | 4 Comments »

ZCE Exam Question: Which PCRE regular expression will match the string PhP5-rocks?

Posted by mcloide on August 12, 2008

Which PCRE regular expression will match the string PhP5-rocks?

Answer…

[ ]

/^[hp1-5]*\-.*/i

[ ]

/[hp1-5]*\-.?/

[ ]

/[hp][1-5]*\-.*/

[ ]

/[PhP]{3}[1-5]{2,3}\-.*$/

[ ]

/[a-z1-5\-]*/

I got intrigued with this question. At first it seems too easy, but it requires a little understanding from PHP’s PCRE functions to really understand it.

If you do the following code and execute it:

<?php

$string = 'PhP5-rocks';

print_r(preg_match(‘/^[hp1-5]*\-.*/i’,$string,$matches,PREG_OFFSET_CAPTURE));
print_r($matches);

print_r(preg_match(‘/[hp1-5]*\-.?/’,$string,$matches,PREG_OFFSET_CAPTURE));
print_r($matches)

print_r(preg_match(‘/[hp][1-5]*\-.*/’,$string,$matches,PREG_OFFSET_CAPTURE));
print_r($matches);

print_r(preg_match(‘/[PhP]{3}[1-5]{2,3}\-.*$/’,$string,$matches,PREG_OFFSET_CAPTURE));
print_r($matches);

print_r(preg_match(‘/[a-z1-5\-]*/’,$string,$matches,PREG_OFFSET_CAPTURE));
print_r($matches);
?>

The first, second and last print_r will print 1 at the screen and the remaining will print 0. If you read preg_match function manual you will see that preg_match returns 1 or 0 always, being 1 as it has matched a string that matches that regex that was given to test. Considering this, you would have 3 correct answers for this puzzle but, in fact there’s only one. When you add the refereced parameter $matches and print it, you will see that the only regex that matches the string is the first one.

When I first have answered this question, I have answered wrongly. I did missed the fact that were a capital P on the string (well who knew…), but this helped me to see what’s was wrong and seek for the correct answer.

I will keep you guys posted. More to come with PHP 5 and Zend Certifications.

Posted in PHP, Zend | Tagged: , , , | 2 Comments »