Archive

PHP

Question was “Can anyone recommend a good PHP book?”

I have used both web sites and books over the past 4 years as I learned PHP.

The php website uk.php.net (for the UK mirror) is excellent, especially the user comments, although beware that a number of user comments and tips are wrong. For that reason, it is worthwhile getting a good PHP book too, to cross reference other material. I have a number of books on PHP, but probably a good one to start with is the PHP Bible, published by Wiley. It also has specific focus on MySQL and Oracle.

It is definitely worth using a good book and learn PHP right from the basics, since it is too easy to just assume ‘I know that’ and skip important things.

Some fundamental things that I have seen trip up novices are:

1) PHP tries very hard to figure out what you mean and it can make a guess which may work most of the time, but then suddenly stops and it would take you ages to figure out why. So, ALWAYS use full error logging output, to spot bad practice.

For instance, Constants are literals without a $ sign, to distinguish from variables, but if a constant of that name does not exist, then it will assume you meant a string and left out the quotes. e.g. look at the following snippet

<?php $a = array('TITLE'=>'PHP Tips', 'AUTHOR'=>'Paul');  echo $a[AUTHOR]." "; define('AUTHOR','TITLE'); echo $a[AUTHOR]." "; echo $a['AUTHOR']." "; ?>

Will output:

Paul PHP Tips Paul

Thus, you should use $a[‘AUTHOR’] unless you really do mean to use the constant.

2) Really understand PHP types. They are subtly different from a language such as C. Particularly note the meaning of === and !== which not only tries to compare value but also type, therefore (0 === ‘0’) is false, but (0 == ‘0’) is true. Look at the following example:

<?php $a = array(NULL,'',0,'0');  foreach($a as $i=>$entry) { {{{   echo "$i: ";   echo $entry?"True, ":"False, ";   echo ($entry==0)?"True, ":"False, ";   echo ($entry=='0')?"True, ":"False, ";   echo ($entry==='0')?"True, ":"False, ";   echo ($entry===0)?"True, ":"False, ";   echo " "; } ?>

}}}

This outputs:

0: False, True, False, False, False, 1: False, True, False, False, False, 2: False, True, True, False, True, 3: False, True, True, True, False,

Which may not be what you expected.

— Note in particular that (‘false’ == true) !! DanPope

3) And as with any language, try to avoid using floats wherever possible to avoid rounding bugs, such as:

<?php for($i = 0; $i<10; $i+=0.125) { if ($i == 5) echo "Midway 0.125"; } for($i = 0; $i<10; $i+=0.1) { if ($i == 5) echo "Midway 0.1"; } ?>

Will work for step size of 0.125, but not 0.1. It fails because of the inaccuracy of 0.1 stored as a binary fraction.

I have seen people really get bogged down with this and started using ranges that they check within, instead of checking exact values, but this does not solve the root problem. So, try wherever possible to use integer arithmetic internally and only convert to floats when you need to make the data humanly readable. Most of the time this is possible.

4) Be careful using the single line comment identifier // as it does not quite behave exactly the same as C++ It will ignore any PHP script until the end of the line, OR THE END OF THE SCRIPT. The script is terminated using ?>. The following two programs demonstrate the problem:

<?php echo "Hello world works, and after ?> should display this text "; ?>

Will output:

Hello world works, and after ?> should display this text
<?php echo "Hello world should not have any text here -> "; //echo "Hello world works, and after ?> should not display this text "; ?>

Will output:

Hello world should not have any text here ->  should not display this text "; ?>

Which again, is probably not what you expected.

There are of course many more gotchas, particularly when you move on to html integration, use database connections and use sessions, but those are things people will tend to read up on.

Leave a Reply