[Previous] [Contents] [Next]

Missing output

An uninitialized variable produces no output. This seems obvious, but it can be hard to identify if the problem is a subtle typographic error. Consider this example of a change in case:

$testvariable = "hello";
echo "The value of test is $testVariable";

This produces the string:

The value of test is

The problem can be much harder to identify by visual inspection if the variable is part of a complex operation, such as being used as an array element index, part of <table> output, or as a parameter to a database query.

If output appears but isn't as expected, an uninitialized variable is a possibility. The simplest approach to detecting the error is then to check for a bug by setting error_reporting(E_ALL) at the top of the script as discussed in the last section.

The function isset( ) can also control execution and debug code, because it returns true if the variable exists (even if it's set to NULL or an empty string) and false if it has never been used.

Another related problem involves variable names appearing where values should. This is usually the simple problem of an omitted dollar sign and is easy to fix. For example:

echo "the value of test is test";

This should have been:

echo "the value of test is $test";

If a dollar sign is omitted in a statement such as an assignment or conditional, the PHP interpreter reports a specific parse error with its default error-reporting level.

A similar problem can also occur when single quotes are used instead of double quotes, because single-quoted strings are always output directly, and the string isn't interpreted like a double-quoted string is. For example:

echo 'the value of test is $test';

This produces:

the value of test is $test

It doesn't output the value of the variable $test.

[Previous] [Contents] [Next]