Archive for the 'PHP certification' Category

booleans casted to strings

Tuesday, March 15th, 2005

What happens when a false value is casted to a string?

I.e. what is the result of the following:
< ?php
var_dump((string)false);
?>

The answer is:
string(0) ""

This is good to know and expect when using some string functions that sometimes return a string and sometimes a boolean. Such functions are strstr(), strpos(), etc.

 

alternative if-then-else syntax

Friday, March 11th, 2005

There's a small error in the PHP certification guide in the alternative if-then-else syntax code.

This alternative syntax goes like:
< ?php
if (true) :
echo 'it is true';
else :
echo 'so not true';
endif;
?>

In the example given in the cert guide, the ; after endif is missing, which will cause a parse error.

 

if-then-else

Friday, March 11th, 2005

Overall, the cert guide is pretty comprehensive on the subject of the conditional statements.

One tiny insignificant detail. There is an example that goes like
< ?php
if ($a < 100) {
echo 'Less than 100';
} else {
echo 'More than 100';
}
?>
Well, it's evident that the second message should have been better off like: "More than or equal to 100".

In the next example we see something like:
< ?php
if ($a < 100) {
echo 'Less than 100';
echo "\nNow I can output more than one line";
}
?>
Here we see how for the second echo, we use double quotes. The reasons were explained earlier in the certification guide. In brief, PHP interpreter evaluates double quotes, looking for variables or escape characters like \n. So far, so good. In the following examples though, the \n is omitted, leaving the reader with:
echo 'Less than 100';
echo "Now I can output more than one line";

So the reader might wonder why the first line uses single quotes and the second uses double quotes. I think just being consistent and keeping the \n won't cause such confusion.

As a side note:
Using double quotes only when necessary, sticking to single quotes on all other occasions, is good practice and will result in somewhat faster execution of your PHP applications. It's a good habit to use single quotes. But … don't lose your sleep over it, it's not the silver bullet for PHP apps optimization, there are plenty of other areas for improvement.

 

prefix/postfix increment/decrement

Wednesday, March 9th, 2005

The PHP certification guide is short but perfectly clear on the topic of postfix and prefix operations.
My only remark is that due to the font used in this book, the pre/postfix decrement (--) is displayed as one long dash :(

Also the reader might be wondering if one can combine those operations, like:
< ?php
++$a--;
++$a++;
++($a++);
(++$a)++;
++++$a;
$a----;
?>
Well, these lines are all invalid. And thank God they are! Imagine how hard it would be to read and maintain code that's using such statements.

 

Assignment by reference

Tuesday, March 8th, 2005

In this section, the curious reader might wonder about the combination of assignment by reference and a simple arithmetic operation such as addition. OK, let's take a step back.

Consider this:
< ?php
$a = 10;
$b = $a;
$a = 20;
echo $b;
?>

In this example the output will be 10, because the value of $a was passed to $b and that's it. Whatever happens to $a after that, $b doesn't want to know.
This was an illustration of the most common case in everyday's programming, passing by value.

Now about passing by reference.
< ?php
$a = 10;
$b = &$a;
$a = 20;
echo $b;
?>

In this case the output will be 20, because a reference to the value of $a was assigned to $b. In other words, $b was just a nickname for $a. Continuing with the nickname analogy, let's say Jon is a nickname for the little boy Jonathan. Jonathan went out to play. After the game Jonathan got really dirty. Can we say that Jon was dirty? Yes, we can. It doesn't matter how we refer to that little fella, we won't get any cleaner.

Now how about passing the result of an arithmetic operation as a reference, like this:
< ?php
$a1 = 10;
$a2 = 20;
$b = &$a1 + &$a2;
$a1 = 300;
echo $b;
?>

The answer is - you cannot do this. You'll get a parse error message from the PHP interpreter.
The same will happen if you try:
$b = &($a1 + $a2);
or
$b = &($a1 + 1);
or
$b = 1 + &$a2;

What you can do though is
$b = &$a1 + $a2;
although it will not work as one may expect. In this example $b will become a nickname for $a1 only. $a2 will be ignored completely. In the following example:
< ?php
$a1 = 10;
$a2 = 20;
$b = &$a1 + $a2;
echo $b;
$a1 = 300;
echo $b;
?>

the first echo will print 10, which is the value of $a1 and the second echo will print 300.

In conclusion, the value of only one variable is passed when using reference. Passing by reference cannot be combined with other (e.g. arithmetic) operations.

BTW,
< ?php
$b = &&$a;
?>

will also cause an error. At the end, does it make sense? Passing the reference of the reference to a variable?

 

About the PHP constants

Monday, March 7th, 2005

The reader of the PHP cert guide might be wondering about some aspects of the PHP constants, because the constants section of the book doesn't go into as much detail as it goes for other sections like variables for example.

1. It's not necassary to name the constants always in upper case. It is, however, a good practice, kind of an unspoken convension. You'll make your code easier to read (and maintain) if you always use upper case.

2. You don't have to use double quotes when defining a constant. In fact, when you don't have a reason to use double quotes, always use single quotes, it's better in terms of performance.

En example to illustrate the two points above:
< ?php
define ('something', 123);
echo something;
?>

3. Constants names are case-sensitive. So the following will not work as expected:
< ?php
define ('something', 123);
echo someThing;
?>

4. You cannot include a constant's value as part of a string as you can do with a variable. Here's an illustration:
< ?php
$var = 'test';
define ('CONST', 123);
echo "testing $var variable";
echo "testing {$var} variable";
echo "testing SOMETHING constant";
echo "testing {SOMETHING} constant";
?>

This will result in:
testing test variable
testing test variable
testing SOMETHING constant
testing {SOMETHING} constant

 

Smart quotes

Monday, March 7th, 2005

Smart quotes are more trouble than they're worth. To me, they should be banned forever from any publication.
Unfortunatelly, the PHP certification guide is using smart quotes even in the code, I hope they are not confusing the people that are just getting started with PHP. The most confusing, I think, is the example where the text explains how quotes can be escaped in strings.

So, to all readers of the PHP cetrtification giude:
Don't pay attention to the direction of the smart quotes!


and
'
are all born equal.
Same goes for


and
"

 

First off - some links

Monday, March 7th, 2005

The sites
Zend PHP Certification - the official homepage of the exam
http://www.zend.com/store/education/certification/zend-php-certification.php

php|architect's Zend Certification pages
http://www.phparch.com/cert/

The books
Amazon.com: Books: PHP Certification Study Guide
http://www.amazon.com/exec/obidos/tg/detail/-/0672327090/…

Amazon.com: Books: The Zend PHP Certification Practice Test Book - Practice Questions for the Zend Certified Engineer Exam
http://www.amazon.com/exec/obidos/tg/detail/-/0973589884/??¦

 

Let's get, let's get, let's get started

Monday, March 7th, 2005

I plan to take the Zend certification exam so I decided it might be a good idea to share some thoughts along the way. I hope it can help others who are decided or thinking about getting certified.

I think I'll be mainly commenting on the certification book and will probably try to add to the information found in it.