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.

The other type_spec specifying characters are given in Table 14-2.<br />

Table 14-2. Type specification characters<br />

Character Description<br />

l Long<br />

d Double<br />

s String (with possible NUL-bytes) and its length<br />

b Boolean, stored in zend_bool<br />

r Resource (stored in zval)<br />

a Array<br />

o Object (of any type)<br />

O Object (of specific type, specified by class entry)<br />

z The actual zval<br />

The modifiers that can follow each of these are given in Table 14-3.<br />

Table 14-3. Type specification modifiers<br />

Modifier Description<br />

| This indicates that all remaining parameters will be optional. Remember to initialize these yourself if they are<br />

not passed by the user. These functions will not put any default values in the parameters.<br />

/ This indicates that the preceding parameter should be separated from the calling parameter, in case you wish to<br />

modify it locally in the function without modifying the original calling parameter.<br />

! This applies only to zval parameters (a, o, O, r, and z) and indicates that the parameter it follows can be<br />

passed a NULL. If the user does pass a NULL, the resulting container is set to NULL.<br />

A Simple Example<br />

The following code gets a long (all integers in <strong>PHP</strong> are longs), a string, and an<br />

optional double (all floating-point values in <strong>PHP</strong> are double-precision):<br />

long l;<br />

char *s;<br />

int s_len;<br />

double d = 0.0;<br />

if (zend_parse_parameters(ZEND_NUM_ARGS( ) TSRMLS_CC, "ls|d", &l, &s, &s_len)<br />

== FAILURE) return;<br />

From a <strong>PHP</strong> script, this function might be called like this:<br />

$num = 10; $desc = 'This is a test'; $price = 69.95;<br />

add_item($num, $desc); // without the optional third argument<br />

add_item($num, $desc, $price); // with the optional third argument<br />

This results in long l being set to 10, char *s containing the string “This is a Test”, and<br />

s_len being set to 14. For the first call, double d maintains the default 0.0 value that<br />

you set, but in the second call, where the user provides an argument, it is set to 69.95.<br />

336 | Chapter 14: Extending <strong>PHP</strong><br />

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

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!