21.3 Returning Strings or Numbers from a Function


You want to return a string or a number from a function.

Technique

Use one of the custom macros, or access the special return_value structure to add your value.

Returning a string:

  1. Use either the RETURN_STRING() or RETURN_STRINGL() macro:

     RETURN_STRING(str, 1); 

    or

     RETURN_STRINGL(str, len, 1); 
  2. Assign it directly to the return value itself:

     ZVAL_STRING(return_value, str, 1); 

    or

     ZVAL_STRINGL(return_value, str, len, 1); 

Returning a long:

  1. Use the RETURN_LONG() macro:

     RETURN_LONG(num); 
  2. Assign it directly:

     ZVAL_LONG(return_value, num); 

Returning a double:

  1. Use the RETURN_DOUBLE() macro:

     RETURN_DOUBLE(num); 
  2. Assign it directly:

     ZVAL_DOUBLE(return_value, num); 

Comments

The preceding are the different ways to return a string, a long, and a double. Note that when returning strings, the syntax (not the definition) of the macro is as follows :

 RETURN_STRING(char *str, int duplicate); 

The parameter duplicate should always be set to 1 , except in some extremely rare cases. The second approach to returning a string is the same as the macro way; in fact, the RETURN_STRING() macro is basically identical to the code given in the second approach.

The RETURN_STRINGL() macro should be used when you want to specify the string length to return. Its syntax (again, not its definition) is the following:

 RETURN_STRINGL(char *str, int len, int duplicate); 

If you want to return true or false, use the RETURN_TRUE and RETURN_FALSE definitions (self explanatory). If you want to return null, use either an empty return or the RETURN_NULL() macro.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net