Re: [PHP-DEV] New Function: is_scalar From: Zak Greant (zak <email protected>)
Date: 12/16/00

Hi Andi,

Thanks for the review! After thinking about it, I believe that NULL values
should not be considered scalars.

Currently, undefined variables will test as being scalars if passed to this
function. This is probably not the desired behavior. AFAIK, an undefined
variable is an undefined variable - not a scalar waiting to happen.

In addition, using an undefined variable generates a warning (if your error
reporting is set high enough). Because of this, we should probably not have
the function return true for this case. I have not looked in detail, but I
would guess that the only constructs that handle unset variables without
generating a warning are isset() and empty() - and they are meant to
explicitly deal with unset variables.

 --zak

Andi Gutmans wrote:
> Zak,
>
> It seems fine.
> Only one issue I'm not 100% sure of is if we want IS_NULL to be a scalar?
> In general the way I see IS_NULL is that it is no type really (it's always
> false, has no value and can be told apart from bool(false) and isset()
> returns false).
> Do most people who would use this function want IS_NULL to be a scalar
> because of the code which will follow this?
>
> Andi
>
> At 09:35 PM 12/15/00 -0700, Zak Greant wrote:
> >I recently submitted a patch for review - given that there was no
feedback,
> >I assume that all is well with it. ;)
> >Unless someone objects, I will submit the modifications to the codebase
> >tomorrow at about 1400 GMT / 1600 Israeli time.
> >
> >Once again - for your viewing pleasure - here is my original 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
> >
> >PS. I do realize that the codebase has changed since I did the diffs --
I
> >will make sure to update the most recent version of the files before I
> >commit anything.
> >
> >
> >--
> >PHP Development Mailing List <http://www.php.net/>
> >To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
> >For additional commands, e-mail: php-dev-help <email protected>
> >To contact the list administrators, e-mail: php-list-admin <email protected>
>
> ---
> Andi Gutmans <andi <email protected>>
> http://www.zend.com/
>
> --
> PHP Development Mailing List <http://www.php.net/>
> To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
> For additional commands, e-mail: php-dev-help <email protected>
> To contact the list administrators, e-mail: php-list-admin <email protected>
>

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
For additional commands, e-mail: php-dev-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>