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.

Table 14-4. RETURN-related convenience macros<br />

RETURN_RESOURCE(int r) RETVAL_RESOURCE(int r)<br />

RETURN_BOOL(int b) RETVAL_BOOL(int b)<br />

RETURN_NULL( ) RETVAL_NULL( )<br />

RETURN_LONG(int l) RETVAL_LONG(int l)<br />

RETURN_DOUBLE(double d) RETVAL_DOUBLE(double d)<br />

RETURN_STRING(char *s, int dup) RETVAL_STRING(char *s, int dup)<br />

RETURN_STRINGL(char *s, int l, int dup) RETVAL_STRINGL(char *s, int l, int dup)<br />

RETURN_EMPTY_STRING( ) RETVAL_EMPTY_STRING( )<br />

RETURN_FALSE RETVAL_FALSE<br />

RETURN_TRUE RETVAL_TRUE<br />

Arrays<br />

To return an array from a function in your extension, initialize return_value to be an<br />

array and then fill it with elements. For example, this returns an array with “123” in<br />

position 0:<br />

<strong>PHP</strong>_FUNCTION(my_func) {<br />

array_init(return_value);<br />

add_index_long(return_value, 0, 123);<br />

}<br />

Call your function from a <strong>PHP</strong> script like this:<br />

$arr = my_func( ); // $arr[0] holds 123<br />

To add a string element to the array:<br />

add_index_string(return_value, 1, "thestring", 1);<br />

This would result in:<br />

$arr[1] = "thestring"<br />

If you have a static string whose length you know already, use the add_index_<br />

stringl( ) function:<br />

add_index_stringl(return_value, 1, "abc", 3, 1);<br />

The final argument specifies whether or not the string you provide should be copied.<br />

Normally, you would set this to 1. The only time you wouldn’t is when you have<br />

allocated the memory for the string yourself, using one of <strong>PHP</strong>’s emalloc( )-like functions.<br />

For example:<br />

char *str;<br />

str = estrdup("abc");<br />

add_index_stringl(return_value, 1, str, 3, 0);<br />

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

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

Returning Values | 339

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

Saved successfully!

Ooh no, something went wrong!