PHP - like most programming languages / scripting languages - will check for syntax errors itself. Briefly, these consist of simple typing mistakes like eco instead of echo; missing {} brackets (construct not closed properly); missing semi-colons ; at the end of a line; and not closing strings properly. Strings are particularly common errors because a lot of PHP can be echoing HTML to the browser and HTML needs to use quotes around certain elements. You MUST escape each and every quotation mark " throughout the echoed code. This is particularly troublesome when you cannot simply do a search and replace on all " characters - you need one unescaped " at the start and end of every PHP string!
Logical errors are harder to spot as PHP may not raise an error at all. The first one to look at involves each and every division calculation the script performs. You must ensure that each division operation is preceded by a check that you are not dividing by zero. Even if your code doesn't take any values directly from the user, it is still worth adding a simple check for zero:
if ($width==0) {echo "Error: width is zero!";}
else { # perform your division calculation
$height = $area / $width; }
Infinite loops are seldom programmed directly, more commonly an infinite loop will result from an erroneous value being entered into a normally well-behaved loop. Such infinite loops will never exit and your script will continue to run for as long as the server runs. On Windows platforms this could lead to a lack of resources and a system crash. Even on platforms where an enforced time limit is set for PHP script execution, your visitor may receive no warning or error message, just a blank screen, so all loops that take input from outside the script must be protected from erroneous input. For example, consider this loop - if the value entered is negative or if some other calculation within the loop makes $value negative, the loop becomes infinite:
while ($value != 0) {$value--;}
This error is relatively easy to solve, the while condition needs to be changed to while ($value > 0) {}. This way, if $value becomes negative, the loop will exit. This could, however, cause unexpected effects elsewhere in the script if $value is used in further calculations that expect a positive value. You may need to specifically write an error handler for situations where $value becomes negative, either resetting the value to a positive default or exiting the script safely. Another consideration is where subsequent code checks to see if $value exists - if the loop exits with a negative value instead of zero, the script will believe the variable exists when it should find the variable empty.
Porting code or extending an existing script is also a hot-bed of bugs. The original script may have been written with all kinds of unwritten assumptions about the values being processed. Changing the function or extending the capability of the script can cause unexpected changes in later code. These can be the hardest errors to find because you must fully understand the previous (working) script before tackling the bugs in the extended/ported one. Two rules can protect you here: Comment your code thoroughly and use sensible variable names! A few weeks/months down the line are you really going to understand what $a contains? If you name it $accepted won't that be easier? If you add a comment at the top of the script that explains that $accepted is only to hold either zero or one, it'll make life even easier. Don't go overboard though, variables like $boolean_accepted_value_for_register will only increase the rate of typing errors!
These pages use "include" files to ensure a consistent feel to each page. The PHP file itself is not much more than a list of variables containing the data for each page. The script then 'includes' another PHP file which exports the appropriate code, XML, HTML4 or, for some pages, WML. These include files can be a source of bugs if you include a file twice. This may not seem likely, but if you include file1.php in file2.php, you may not notice that both file1 and file2 need to include file3.php! If any functions are declared in the duplicated include file, PHP will raise an error. However, even without such an error, the duplicated code could have unexpected effects. To safeguard against this, take a tip from C++ and other programming languages that rely heavily on included files like headers. Declare a unique variable in the included file and then before including the file in another script, check that the variable doesn't already exist. If it does, you've already included the file. In the above example of files 1, 2 and 3, if you declare $FILE3INCLUDED=1; in file3.php, then you can check:
if (!$FILE3INCLUDED) {include 'file3.php';}
Also pay attention to WHERE you put the include 'myfile.php'; command. PHP will do two things at the include command: It will close any current PHP script using ?> and open a new PHP script tag AFTER the included file. The included file will then INHERIT all variables at the existing values at that point of the original script. All include files must therefore open and close the PHP tag as appropriate - or you're precious included code will be output to the browser as text! Secondly, the include statement must be placed in the original file at the point where all necessary variables have been declared and set to necessary values and BEFORE any code that modifies or unsets those variables.
One final point on include files, some programmers use a common include file called common.inc but not all servers are set-up to execute .inc files as PHP so if a visitor deliberately types www.yoursite.com/common.inc into the browser, they could get an eyeful of all your code! It's safer to use common.php if you want one file to hold certain variables common to the whole site - that way you can be sure that any such deliberate attempt to peer into your code will result in a blank screen (or a nasty warning if you choose to put some code in common.php to that effect!)
This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for
copying conditions.