New Function: is_scalar

From: Date: Thu, 14 Dec 2000 23:30:08 +0000
Subject: New Function: is_scalar
Groups: php.dev 
Request: Send a blank email to php-dev+get-41420@lists.php.net to get a copy of this message
Hello All, I would like add a trivial little function that tests to see if a value is a scalar. I have written the function and tested it on Mandrake 7.1 with good results. (The function seemed very straightforward to implement - I based it on is_numeric()) Does anyone object to this function being added to the codebase? Personally, I would use this function to replace code snippets like: if (is_array ($foo) || is_object ($foo) || is_resource ($foo)) return FALSE; Would anyone care to quickly review my patch and ensure that it does not do anything silly. :) --- basic_functions.h Wed Nov 29 08:37:38 2000 +++ basic_functions.h.zak Thu Dec 14 16:09:17 2000 @@ -73,6 +73,7 @@ PHP_FUNCTION(is_long); PHP_FUNCTION(is_double); PHP_FUNCTION(is_numeric); +PHP_FUNCTION(is_scalar); PHP_FUNCTION(is_string); PHP_FUNCTION(is_array); PHP_FUNCTION(is_object); --- basic_functions.c Wed Dec 13 16:02:09 2000 +++ basic_functions.c.zak Thu Dec 14 16:09:10 2000 @@ -319,6 +319,7 @@ PHP_FE(is_double, first_arg_allow_ref) PHP_FALIAS(is_real, is_double, first_arg_allow_ref) PHP_FE(is_numeric, NULL) + PHP_FE(is_scalar, NULL) PHP_FE(is_string, first_arg_allow_ref) PHP_FE(is_array, first_arg_allow_ref) PHP_FE(is_object, first_arg_allow_ref) @@ -1403,6 +1404,35 @@ } } /* }}} */ + + +/* {{{ proto bool is_scalar(mixed value) + Returns true if value is a scalar value */ +PHP_FUNCTION(is_scalar) +{ + zval **arg; + int result; + + if (ZEND_NUM_ARGS() !=1 || zend_get_parameters_ex(1, &arg) == FAILURE) { + WRONG_PARAM_COUNT; + } + + switch (Z_TYPE_PP(arg)) { + case IS_BOOL: + case IS_DOUBLE: + case IS_LONG: + case IS_NULL: + case IS_STRING: + RETURN_TRUE; + break; + + default: + RETURN_FALSE; + break; + } +} +/* }}} */ + /* 1st arg = error message Thanks! --zak

« previous php.dev (#41420) next »