05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

If you compile the Apache module version of <strong>PHP</strong> with the --enable-memory-limit<br />

switch, it will add the script’s peak memory usage to the Apache r->notes table. You<br />

can access this information from other Apache modules, such as mod_log_config.<br />

Add this string to your Apache LogFormat line to log the peak number of bytes a<br />

script used:<br />

%{mod_php_memory_usage}n<br />

If you’re having problems with a module allocating too much memory and grinding<br />

your system into the ground, build <strong>PHP</strong> with the memory-limit option enabled. This<br />

makes <strong>PHP</strong> heed the memory_limit directive in your php.ini file, terminating a script if<br />

it tries to allocate more memory than the specified limit. This results in errors like<br />

this:<br />

Fatal error: Allowed memory size of 102400 bytes exhausted at ...<br />

(tried to allocate 46080 bytes) in /path/script.php on line 35<br />

The pval/zval Data Type<br />

Throughout the <strong>PHP</strong> source code, you will see references to both pval and zval.<br />

They are the same thing and can be used interchangeably. The pval/zval is the basic<br />

data container in <strong>PHP</strong>. All data that is passed between the extension API and the<br />

user-level script is passed in this container. You can dig into the header files further<br />

yourself, but in simple terms, this container is a union that can hold either a long, a<br />

double, a string including the string length, an array, or an object. The union looks<br />

like this:<br />

typedef union _zvalue_value {<br />

long lval;<br />

double dval;<br />

struct {<br />

char *val;<br />

int len;<br />

} str;<br />

HashTable *ht;<br />

zend_object obj;<br />

} zvalue_value;<br />

The main things to learn from this union are that all integers are stored as longs, all<br />

floating-point values are stored in double-precision, and every string has an associated<br />

string length value, which, if properly checked everywhere, makes strings in<br />

<strong>PHP</strong> binary-safe. * Strings do not need to be null-terminated, but since most thirdparty<br />

libraries expect null-terminated strings it is a good idea to always null-terminate<br />

any string you create.<br />

* Binary-safe, sometimes referred to as 8-bit clean, means that a string can contain any of the 256 ASCII values,<br />

including the ASCII value 0.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

The pval/zval Data Type | 331

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!