New Perspectives On Web Design

(C. Jardin) #1
By Paul Tero CHAPTER 8

biT flagS


Using bit flags for error_reporting avoids having 15 separate arguments
to the function for each type of error. Bit flags can also be useful in your
own code. To use them, you need to define some constants, use the bit OR
operator | when calling the function and the bit AND operator & within
the function. Here’s a simple PHP example using bit flags to tell a function
called showproduct which product properties to display:


<?
define ('PRODUCT_NAME', 1);
define ('PRODUCT_PRICE', 2);
function showproduct ($product, $flags) {
if ($flags & PRODUCT_NAME) echo $product['name'];
if ($flags & PRODUCT_PRICE) echo ': $'. $product['price'];
}
$product = array ('name'=>'Widget 3000', 'price'=>10);
showproduct ($product, PRODUCT_NAME | PRODUCT_PRICE);
?>


This will display “Widget 3000: $10” in the browser. This is a rather
superficial example of bit flags. Usually there are far deeper, more constant
system processes.


infiniTe looPS
PHP’s error reporting may struggle with one class of error: an infinite loop. A
loop may just keep executing until it hits PHP’s time limit, which is usually
30 seconds (PHP’s max_execution_time setting), causing a fatal error. Or
if the loop allocates new variables or calls functions, it may keep going until
PHP runs out of workable memory (PHP’s memory_limit setting).
It may, however, cause the Apache child process to crash, which means
nothing will get reported, and you’ll just see a blank or partial page. This
type of error is increasingly rare, as PHP and Apache are now very mature
and can detect and handle runaway problems like this. But if you are about
to bang your head against the wall in frustration because none of the

Free download pdf