I'm running behind schedule this month, so I'll put off
my column on Ada and Open Source community conflict for a look at PHP.
If people learning from their mistakes, then PHP must be a
great teacher.
It's always been my opinion that computer languages should
work with the developer, not against him. That doesn't mean that hostile
languages can't be successful, or that developers can't abuse them. Here are
10 of my favourite PHP errors.
- The End-of-File Dollar Sign
Leaving out a closing curly bracket causes an
"unexpected $ at (end-of_file)" error. For example,
if ( x == 1 ) {
if ( y == 2 ) {
}
The only efficient way of debugging this one, since the
line number is useless, is to use an IDE that finds the offending line
for you.
- The Unquotable Quotes
Single and double quotes can be used to quote each other, but that's seldom
useful when generating HTML. Consider the case of PHP producing JavaScript
producing HTML. There is no way to easily quote any string.
print "document.write(\"<A ONCLICK=\\\"alert('Hello World')\\\">\")\n";
It would have been even worse if I used double quotes in the JavaScript
statement.
- The Novice Narrow Database Class
The first thing many novice PHP developers do when they learn
about object oriented programming is to create functions or classes
surrounding basic database functions. They are right that SQL and PHP don't mix
very well--SQL doesn't mix with most languages well. But their solution is to
abstract the syntax instead of encapsulating a concept, causing multiple inheritance
problems. Instead of an object around running queries, try an object around a
set of customer or supplier tables.
- The Phantom One Print
print "This is a test";
print
print "This is another line\n";
Results in "This is a test1". print returns "1" because the second print
is
print print "This is another line\n";
and print always returns one when used as a
function--even though using it as a function is nonsense. This should be
print "This is a test";
print "\n";
print "This is another line\n";
It looks like BASIC, but it doesn't work like BASIC.
- Semicolon or Period--That is the Question
PHP is a language where nonsense can be executed. The string "X" can
be executed like a command (it does nothing). But when building up a database query,
perhaps adding some new conditions, and you get the following problem.
$query = "SELECT * FROM TABLE " .
"WHERE i > j ";
"AND l > m";
"AND l > m"; is a legal PHP statement. So there's no
indication of a punctuation problem except the SQL query doesn't return the right results.
- (Not) All Things Are (Double) Equal
In SQL, equal is "=". In PHP, it's "==". Sure, not equals in SQL is "<>"
and is "!=" in PHP, but a developer must constantly remember what language is programming
in.
if ( x = 7 ) { // this is PHP not SQL!
// this will always happen
}
- Bad Boy Booleans
What is truth? In PHP, logical truth works differently than in Perl or C.
I can't find the example I wanted to share but
http://ca.php.net/manual/en/language.types.boolean.php has many examples of strange logical operations under PHP.
- The Missing Quote Hunt
Leaving out a quote mark often leads to error messages that
are not on the offending line. Depending on the use of quotations, the error
may be several lines away from the offending line.
$query = "SELECT first_name, last_name FROM users .
"WHERE id = " . $user_id;
The error message is never "missing quote mark" but some other
obtuse message about an unexpected token.
- The Database Equality Type Tango
When databases are accessed in PHP, the results are all
string values, even if the database columns are numeric. For some kinds
of activities, like printing out the results, the type of data doesn't
matter. In some cases, it does.
$db_data = $db_row['int_value'];
if ( $db_data == -1 ) {
// doesn't run if $db_data is -1
}
In this example, $db_data is a string result from a database
query. The "==" operator is comparing a string to a number: even if $db_data
is "-1", the instructions inside the if statement will not run because "-1"
(a string) is not equal to -1 (a number). Either a typecast over the
equivalence operator "===" must be used as a workaround.
$db_data = isset( $db_row['int_value'] ) ? (int)($db_row['int_value']) : null;
if ( $db_data == -1 ) {
// will run if $db_data is -1
}
What works in Perl or C doesn't necessarily work in PHP, even
though the statements look the same.
- The Extra Spacing Layout Mystery
Spacing is significant in HTML, especially when the end-of-line
counts as white space. This is particularly a problem with table tags.
<TD>
<?php
...
?>
</TD>
In the above example, there's a space before and after the script results
caused by the end-of-line between the <TD> and the <?php, and the ?> and </TD> tags.
The more correct, but harder to read, approach is
<TD><?php
...
?></TD>
or to put the calculations elsewhere and only write the
results with a short PHP tag.
<TD><?php print $result ?></TD>
A language with no limits is a language with no limits on
errors, either.
Talk back on the Linux Cafe.