root/ext/standard/type.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. PHP_FUNCTION
  2. PHP_FUNCTION
  3. PHP_FUNCTION
  4. PHP_FUNCTION
  5. PHP_FUNCTION
  6. PHP_FUNCTION
  7. php_is_type
  8. PHP_FUNCTION
  9. PHP_FUNCTION
  10. PHP_FUNCTION
  11. PHP_FUNCTION
  12. PHP_FUNCTION
  13. PHP_FUNCTION
  14. PHP_FUNCTION
  15. PHP_FUNCTION
  16. PHP_FUNCTION
  17. PHP_FUNCTION
  18. PHP_FUNCTION

   1 /*
   2    +----------------------------------------------------------------------+
   3    | PHP Version 7                                                        |
   4    +----------------------------------------------------------------------+
   5    | Copyright (c) 1997-2016 The PHP Group                                |
   6    +----------------------------------------------------------------------+
   7    | This source file is subject to version 3.01 of the PHP license,      |
   8    | that is bundled with this package in the file LICENSE, and is        |
   9    | available through the world-wide-web at the following url:           |
  10    | http://www.php.net/license/3_01.txt                                  |
  11    | If you did not receive a copy of the PHP license and are unable to   |
  12    | obtain it through the world-wide-web, please send a note to          |
  13    | license@php.net so we can mail you a copy immediately.               |
  14    +----------------------------------------------------------------------+
  15    | Author: Rasmus Lerdorf <rasmus@php.net>                              |
  16    +----------------------------------------------------------------------+
  17 */
  18 
  19 /* $Id$ */
  20 
  21 #include "php.h"
  22 #include "php_incomplete_class.h"
  23 
  24 /* {{{ proto string gettype(mixed var)
  25    Returns the type of the variable */
  26 PHP_FUNCTION(gettype)
  27 {
  28         zval *arg;
  29 
  30         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
  31                 return;
  32         }
  33 
  34         switch (Z_TYPE_P(arg)) {
  35                 case IS_NULL:
  36                         RETVAL_STRING("NULL");
  37                         break;
  38 
  39                 case IS_FALSE:
  40                 case IS_TRUE:
  41                         RETVAL_STRING("boolean");
  42                         break;
  43 
  44                 case IS_LONG:
  45                         RETVAL_STRING("integer");
  46                         break;
  47 
  48                 case IS_DOUBLE:
  49                         RETVAL_STRING("double");
  50                         break;
  51 
  52                 case IS_STRING:
  53                         RETVAL_STRING("string");
  54                         break;
  55 
  56                 case IS_ARRAY:
  57                         RETVAL_STRING("array");
  58                         break;
  59 
  60                 case IS_OBJECT:
  61                         RETVAL_STRING("object");
  62                 /*
  63                    {
  64                    char *result;
  65                    int res_len;
  66 
  67                    res_len = sizeof("object of type ")-1 + Z_OBJCE_P(arg)->name_length;
  68                    spprintf(&result, 0, "object of type %s", Z_OBJCE_P(arg)->name);
  69                    RETVAL_STRINGL(result, res_len);
  70                    efree(result);
  71                    }
  72                  */
  73                         break;
  74 
  75                 case IS_RESOURCE:
  76                         {
  77                                 const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg));
  78 
  79                                 if (type_name) {
  80                                         RETVAL_STRING("resource");
  81                                         break;
  82                                 }
  83                         }
  84 
  85                 default:
  86                         RETVAL_STRING("unknown type");
  87         }
  88 }
  89 /* }}} */
  90 
  91 /* {{{ proto bool settype(mixed var, string type)
  92    Set the type of the variable */
  93 PHP_FUNCTION(settype)
  94 {
  95         zval *var;
  96         char *type;
  97         size_t type_len = 0;
  98 
  99         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &var, &type, &type_len) == FAILURE) {
 100                 return;
 101         }
 102 
 103         ZVAL_DEREF(var);
 104         if (!strcasecmp(type, "integer")) {
 105                 convert_to_long(var);
 106         } else if (!strcasecmp(type, "int")) {
 107                 convert_to_long(var);
 108         } else if (!strcasecmp(type, "float")) {
 109                 convert_to_double(var);
 110         } else if (!strcasecmp(type, "double")) { /* deprecated */
 111                 convert_to_double(var);
 112         } else if (!strcasecmp(type, "string")) {
 113                 convert_to_string(var);
 114         } else if (!strcasecmp(type, "array")) {
 115                 convert_to_array(var);
 116         } else if (!strcasecmp(type, "object")) {
 117                 convert_to_object(var);
 118         } else if (!strcasecmp(type, "bool")) {
 119                 convert_to_boolean(var);
 120         } else if (!strcasecmp(type, "boolean")) {
 121                 convert_to_boolean(var);
 122         } else if (!strcasecmp(type, "null")) {
 123                 convert_to_null(var);
 124         } else if (!strcasecmp(type, "resource")) {
 125                 php_error_docref(NULL, E_WARNING, "Cannot convert to resource type");
 126                 RETURN_FALSE;
 127         } else {
 128                 php_error_docref(NULL, E_WARNING, "Invalid type");
 129                 RETURN_FALSE;
 130         }
 131         RETVAL_TRUE;
 132 }
 133 /* }}} */
 134 
 135 /* {{{ proto int intval(mixed var [, int base])
 136    Get the integer value of a variable using the optional base for the conversion */
 137 PHP_FUNCTION(intval)
 138 {
 139         zval *num;
 140         zend_long base = 10;
 141 
 142         if (ZEND_NUM_ARGS() != 1 && ZEND_NUM_ARGS() != 2) {
 143                 WRONG_PARAM_COUNT;
 144         }
 145 #ifndef FAST_ZPP
 146         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &num, &base) == FAILURE) {
 147                 return;
 148         }
 149 #else
 150         ZEND_PARSE_PARAMETERS_START(1, 2)
 151                 Z_PARAM_ZVAL(num)
 152                 Z_PARAM_OPTIONAL
 153                 Z_PARAM_LONG(base)
 154         ZEND_PARSE_PARAMETERS_END();
 155 #endif
 156 
 157         if (Z_TYPE_P(num) != IS_STRING) {
 158                 RETVAL_LONG(zval_get_long(num));
 159         } else {
 160                 RETVAL_LONG(ZEND_STRTOL(Z_STRVAL_P(num), NULL, base));
 161         }
 162 }
 163 /* }}} */
 164 
 165 /* {{{ proto float floatval(mixed var)
 166    Get the float value of a variable */
 167 PHP_FUNCTION(floatval)
 168 {
 169         zval *num;
 170 
 171         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &num) == FAILURE) {
 172                 return;
 173         }
 174 
 175         RETURN_DOUBLE(zval_get_double(num));
 176 }
 177 /* }}} */
 178 
 179 /* {{{ proto bool boolval(mixed var)
 180    Get the boolean value of a variable */
 181 PHP_FUNCTION(boolval)
 182 {
 183         zval *val;
 184 
 185         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &val) == FAILURE) {
 186                 return;
 187         }
 188 
 189         RETURN_BOOL(zend_is_true(val));
 190 }
 191 /* }}} */
 192 
 193 /* {{{ proto string strval(mixed var)
 194    Get the string value of a variable */
 195 PHP_FUNCTION(strval)
 196 {
 197         zval *num;
 198 
 199 #ifndef FAST_ZPP
 200         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &num) == FAILURE) {
 201                 return;
 202         }
 203 #else
 204         ZEND_PARSE_PARAMETERS_START(1, 1)
 205                 Z_PARAM_ZVAL(num)
 206         ZEND_PARSE_PARAMETERS_END();
 207 #endif
 208 
 209         RETVAL_STR(zval_get_string(num));
 210 }
 211 /* }}} */
 212 
 213 static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type)
 214 {
 215         zval *arg;
 216 
 217 #ifndef FAST_ZPP
 218         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
 219                 RETURN_FALSE;
 220         }
 221         ZVAL_DEREF(arg);
 222 #else
 223         ZEND_PARSE_PARAMETERS_START(1, 1)
 224                 Z_PARAM_ZVAL_DEREF(arg)
 225         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
 226 #endif
 227 
 228         if (Z_TYPE_P(arg) == type) {
 229                 if (type == IS_OBJECT) {
 230                         zend_class_entry *ce = Z_OBJCE_P(arg);
 231                         if (ZSTR_LEN(ce->name) == sizeof(INCOMPLETE_CLASS) - 1
 232                                         && !memcmp(ZSTR_VAL(ce->name), INCOMPLETE_CLASS, sizeof(INCOMPLETE_CLASS) - 1)) {
 233                                 RETURN_FALSE;
 234                         }
 235                 } else if (type == IS_RESOURCE) {
 236                         const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg));
 237                         if (!type_name) {
 238                                 RETURN_FALSE;
 239                         }
 240                 }
 241                 RETURN_TRUE;
 242         } else {
 243                 RETURN_FALSE;
 244         }
 245 }
 246 
 247 
 248 /* {{{ proto bool is_null(mixed var)
 249    Returns true if variable is null
 250    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
 251 PHP_FUNCTION(is_null)
 252 {
 253         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
 254 }
 255 /* }}} */
 256 
 257 /* {{{ proto bool is_resource(mixed var)
 258    Returns true if variable is a resource
 259    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
 260 PHP_FUNCTION(is_resource)
 261 {
 262         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_RESOURCE);
 263 }
 264 /* }}} */
 265 
 266 /* {{{ proto bool is_bool(mixed var)
 267    Returns true if variable is a boolean
 268    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
 269 PHP_FUNCTION(is_bool)
 270 {
 271         zval *arg;
 272 
 273         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
 274                 RETURN_FALSE;
 275         }
 276 
 277         ZVAL_DEREF(arg);
 278         RETURN_BOOL(Z_TYPE_P(arg) == IS_FALSE || Z_TYPE_P(arg) == IS_TRUE);
 279 }
 280 /* }}} */
 281 
 282 /* {{{ proto bool is_int(mixed var)
 283    Returns true if variable is an integer
 284    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
 285 PHP_FUNCTION(is_int)
 286 {
 287         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_LONG);
 288 }
 289 /* }}} */
 290 
 291 /* {{{ proto bool is_float(mixed var)
 292    Returns true if variable is float point
 293    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
 294 PHP_FUNCTION(is_float)
 295 {
 296         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_DOUBLE);
 297 }
 298 /* }}} */
 299 
 300 /* {{{ proto bool is_string(mixed var)
 301    Returns true if variable is a string
 302    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
 303 PHP_FUNCTION(is_string)
 304 {
 305         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_STRING);
 306 }
 307 /* }}} */
 308 
 309 /* {{{ proto bool is_array(mixed var)
 310    Returns true if variable is an array
 311    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
 312 PHP_FUNCTION(is_array)
 313 {
 314         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_ARRAY);
 315 }
 316 /* }}} */
 317 
 318 /* {{{ proto bool is_object(mixed var)
 319    Returns true if variable is an object
 320    Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
 321 PHP_FUNCTION(is_object)
 322 {
 323         php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_OBJECT);
 324 }
 325 /* }}} */
 326 
 327 /* {{{ proto bool is_numeric(mixed value)
 328    Returns true if value is a number or a numeric string */
 329 PHP_FUNCTION(is_numeric)
 330 {
 331         zval *arg;
 332 
 333 #ifndef FAST_ZPP
 334         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
 335                 return;
 336         }
 337 #else
 338         ZEND_PARSE_PARAMETERS_START(1, 1)
 339                 Z_PARAM_ZVAL(arg)
 340         ZEND_PARSE_PARAMETERS_END();
 341 #endif
 342 
 343         switch (Z_TYPE_P(arg)) {
 344                 case IS_LONG:
 345                 case IS_DOUBLE:
 346                         RETURN_TRUE;
 347                         break;
 348 
 349                 case IS_STRING:
 350                         if (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 0)) {
 351                                 RETURN_TRUE;
 352                         } else {
 353                                 RETURN_FALSE;
 354                         }
 355                         break;
 356 
 357                 default:
 358                         RETURN_FALSE;
 359                         break;
 360         }
 361 }
 362 /* }}} */
 363 
 364 /* {{{ proto bool is_scalar(mixed value)
 365    Returns true if value is a scalar */
 366 PHP_FUNCTION(is_scalar)
 367 {
 368         zval *arg;
 369 
 370 #ifndef FAST_ZPP
 371         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
 372                 return;
 373         }
 374 #else
 375         ZEND_PARSE_PARAMETERS_START(1, 1)
 376                 Z_PARAM_ZVAL(arg)
 377         ZEND_PARSE_PARAMETERS_END();
 378 #endif
 379 
 380         switch (Z_TYPE_P(arg)) {
 381                 case IS_FALSE:
 382                 case IS_TRUE:
 383                 case IS_DOUBLE:
 384                 case IS_LONG:
 385                 case IS_STRING:
 386                         RETURN_TRUE;
 387                         break;
 388 
 389                 default:
 390                         RETURN_FALSE;
 391                         break;
 392         }
 393 }
 394 /* }}} */
 395 
 396 /* {{{ proto bool is_callable(mixed var [, bool syntax_only [, string callable_name]])
 397    Returns true if var is callable. */
 398 PHP_FUNCTION(is_callable)
 399 {
 400         zval *var, *callable_name = NULL;
 401         zend_string *name;
 402         char *error;
 403         zend_bool retval;
 404         zend_bool syntax_only = 0;
 405         int check_flags = 0;
 406 
 407         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|bz/", &var,
 408                                                           &syntax_only, &callable_name) == FAILURE) {
 409                 return;
 410         }
 411 
 412         if (syntax_only) {
 413                 check_flags |= IS_CALLABLE_CHECK_SYNTAX_ONLY;
 414         }
 415         if (ZEND_NUM_ARGS() > 2) {
 416                 retval = zend_is_callable_ex(var, NULL, check_flags, &name, NULL, &error);
 417                 zval_dtor(callable_name);
 418                 //??? is it necessary to be consistent with old PHP ("\0" support)
 419                 if (UNEXPECTED(ZSTR_LEN(name)) != strlen(ZSTR_VAL(name))) {
 420                         ZVAL_STRINGL(callable_name, ZSTR_VAL(name), strlen(ZSTR_VAL(name)));
 421                         zend_string_release(name);
 422                 } else {
 423                         ZVAL_STR(callable_name, name);
 424                 }
 425         } else {
 426                 retval = zend_is_callable_ex(var, NULL, check_flags, NULL, NULL, &error);
 427         }
 428         if (error) {
 429                 /* ignore errors */
 430                 efree(error);
 431         }
 432 
 433         RETURN_BOOL(retval);
 434 }
 435 /* }}} */
 436 
 437 /*
 438  * Local variables:
 439  * tab-width: 4
 440  * c-basic-offset: 4
 441  * End:
 442  * vim600: sw=4 ts=4 fdm=marker
 443  * vim<600: sw=4 ts=4
 444  */

/* [<][>][^][v][top][bottom][index][help] */