Back to index

php5  5.3.10
type.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Rasmus Lerdorf <rasmus@php.net>                              |
00016    +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: type.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #include "php.h"
00022 #include "php_incomplete_class.h"
00023 
00024 /* {{{ proto string gettype(mixed var)
00025    Returns the type of the variable */
00026 PHP_FUNCTION(gettype)
00027 {
00028        zval **arg;
00029 
00030        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
00031               return;
00032        }
00033 
00034        switch (Z_TYPE_PP(arg)) {
00035               case IS_NULL:
00036                      RETVAL_STRING("NULL", 1);
00037                      break;
00038 
00039               case IS_BOOL:
00040                      RETVAL_STRING("boolean", 1);
00041                      break;
00042 
00043               case IS_LONG:
00044                      RETVAL_STRING("integer", 1);
00045                      break;
00046 
00047               case IS_DOUBLE:
00048                      RETVAL_STRING("double", 1);
00049                      break;
00050        
00051               case IS_STRING:
00052                      RETVAL_STRING("string", 1);
00053                      break;
00054        
00055               case IS_ARRAY:
00056                      RETVAL_STRING("array", 1);
00057                      break;
00058 
00059               case IS_OBJECT:
00060                      RETVAL_STRING("object", 1);
00061               /*
00062                  {
00063                  char *result;
00064                  int res_len;
00065 
00066                  res_len = sizeof("object of type ")-1 + Z_OBJCE_P(arg)->name_length;
00067                  spprintf(&result, 0, "object of type %s", Z_OBJCE_P(arg)->name);
00068                  RETVAL_STRINGL(result, res_len, 0);
00069                  }
00070                */
00071                      break;
00072 
00073               case IS_RESOURCE:
00074                      {
00075                             char *type_name;
00076                             type_name = zend_rsrc_list_get_rsrc_type(Z_LVAL_PP(arg) TSRMLS_CC);
00077                             if (type_name) {
00078                                    RETVAL_STRING("resource", 1);
00079                                    break;
00080                             }
00081                      }
00082 
00083               default:
00084                      RETVAL_STRING("unknown type", 1);
00085        }
00086 }
00087 /* }}} */
00088 
00089 /* {{{ proto bool settype(mixed var, string type)
00090    Set the type of the variable */
00091 PHP_FUNCTION(settype)
00092 {
00093        zval **var;
00094        char *type;
00095        int type_len = 0;
00096 
00097        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zs", &var, &type, &type_len) == FAILURE) {
00098               return;
00099        }
00100 
00101        if (!strcasecmp(type, "integer")) {
00102               convert_to_long(*var);
00103        } else if (!strcasecmp(type, "int")) {
00104               convert_to_long(*var);
00105        } else if (!strcasecmp(type, "float")) {
00106               convert_to_double(*var);
00107        } else if (!strcasecmp(type, "double")) { /* deprecated */
00108               convert_to_double(*var);
00109        } else if (!strcasecmp(type, "string")) {
00110               convert_to_string(*var);
00111        } else if (!strcasecmp(type, "array")) {
00112               convert_to_array(*var);
00113        } else if (!strcasecmp(type, "object")) {
00114               convert_to_object(*var);
00115        } else if (!strcasecmp(type, "bool")) {
00116               convert_to_boolean(*var);
00117        } else if (!strcasecmp(type, "boolean")) {
00118               convert_to_boolean(*var);
00119        } else if (!strcasecmp(type, "null")) {
00120               convert_to_null(*var);
00121        } else if (!strcasecmp(type, "resource")) {
00122               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot convert to resource type");
00123               RETURN_FALSE;
00124        } else {
00125               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid type");
00126               RETURN_FALSE;
00127        }
00128        RETVAL_TRUE;
00129 }
00130 /* }}} */
00131 
00132 /* {{{ proto int intval(mixed var [, int base])
00133    Get the integer value of a variable using the optional base for the conversion */
00134 PHP_FUNCTION(intval)
00135 {
00136        zval **num;
00137        long arg_base;
00138        int base;
00139 
00140        switch (ZEND_NUM_ARGS()) {
00141               case 1:
00142                      if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &num) == FAILURE) {
00143                             return;
00144                      }
00145                      base = 10;
00146                      break;
00147 
00148               case 2:
00149                      if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zl", &num, &arg_base) == FAILURE) {
00150                             return;
00151                      }
00152                      base = arg_base;
00153                      break;
00154 
00155               default:
00156                      WRONG_PARAM_COUNT;
00157        }
00158 
00159        RETVAL_ZVAL(*num, 1, 0);
00160        convert_to_long_base(return_value, base);
00161 }
00162 /* }}} */
00163 
00164 /* {{{ proto float floatval(mixed var)
00165    Get the float value of a variable */
00166 PHP_FUNCTION(floatval)
00167 {
00168        zval **num;
00169 
00170        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &num) == FAILURE) {
00171               return;
00172        }
00173 
00174        RETVAL_ZVAL(*num, 1, 0);
00175        convert_to_double(return_value);
00176 }
00177 /* }}} */
00178 
00179 /* {{{ proto string strval(mixed var)
00180    Get the string value of a variable */
00181 PHP_FUNCTION(strval)
00182 {
00183        zval **num, *tmp;
00184        zval expr_copy;
00185        int use_copy;
00186 
00187        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &num) == FAILURE) {
00188               return;
00189        }
00190 
00191        zend_make_printable_zval(*num, &expr_copy, &use_copy);
00192        if (use_copy) {
00193               tmp = &expr_copy;
00194               RETVAL_ZVAL(tmp, 0, 0);
00195        } else {
00196               RETVAL_ZVAL(*num, 1, 0);
00197        }
00198 }
00199 /* }}} */
00200 
00201 static void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type)
00202 {
00203        zval **arg;
00204 
00205        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
00206               RETURN_FALSE;
00207        }
00208 
00209        if (Z_TYPE_PP(arg) == type) {
00210               if (type == IS_OBJECT) {
00211                      zend_class_entry *ce;
00212                      if(Z_OBJ_HT_PP(arg)->get_class_entry == NULL) {
00213                      /* if there's no get_class_entry it's not a PHP object, so it can't be INCOMPLETE_CLASS */
00214                             RETURN_TRUE;
00215                      }
00216                      ce = Z_OBJCE_PP(arg);
00217                      if (!strcmp(ce->name, INCOMPLETE_CLASS)) {
00218                             RETURN_FALSE;
00219                      }
00220               }
00221               if (type == IS_RESOURCE) {
00222                      char *type_name;
00223                      type_name = zend_rsrc_list_get_rsrc_type(Z_LVAL_PP(arg) TSRMLS_CC);
00224                      if (!type_name) {
00225                             RETURN_FALSE;
00226                      }
00227               }
00228               RETURN_TRUE;
00229        } else {
00230               RETURN_FALSE;
00231        }
00232 }
00233 
00234 
00235 /* {{{ proto bool is_null(mixed var)
00236    Returns true if variable is null */
00237 PHP_FUNCTION(is_null)
00238 {
00239        php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
00240 }
00241 /* }}} */
00242 
00243 /* {{{ proto bool is_resource(mixed var)
00244    Returns true if variable is a resource */
00245 PHP_FUNCTION(is_resource)
00246 {
00247        php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_RESOURCE);
00248 }
00249 /* }}} */
00250 
00251 /* {{{ proto bool is_bool(mixed var)
00252    Returns true if variable is a boolean */
00253 PHP_FUNCTION(is_bool)
00254 {
00255        php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_BOOL);
00256 }
00257 /* }}} */
00258 
00259 /* {{{ proto bool is_long(mixed var)
00260    Returns true if variable is a long (integer) */
00261 PHP_FUNCTION(is_long)
00262 {
00263        php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_LONG);
00264 }
00265 /* }}} */
00266 
00267 /* {{{ proto bool is_float(mixed var)
00268    Returns true if variable is float point*/
00269 PHP_FUNCTION(is_float)
00270 {
00271        php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_DOUBLE);
00272 }
00273 /* }}} */
00274 
00275 /* {{{ proto bool is_string(mixed var)
00276    Returns true if variable is a string */
00277 PHP_FUNCTION(is_string)
00278 {
00279        php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_STRING);
00280 }
00281 /* }}} */
00282 
00283 /* {{{ proto bool is_array(mixed var)
00284    Returns true if variable is an array */
00285 PHP_FUNCTION(is_array)
00286 {
00287        php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_ARRAY);
00288 }
00289 /* }}} */
00290 
00291 /* {{{ proto bool is_object(mixed var)
00292    Returns true if variable is an object */
00293 PHP_FUNCTION(is_object)
00294 {
00295        php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_OBJECT);
00296 }
00297 /* }}} */
00298 
00299 /* {{{ proto bool is_numeric(mixed value)
00300    Returns true if value is a number or a numeric string */
00301 PHP_FUNCTION(is_numeric)
00302 {
00303        zval **arg;
00304 
00305        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
00306               return;
00307        }
00308 
00309        switch (Z_TYPE_PP(arg)) {
00310               case IS_LONG:
00311               case IS_DOUBLE:
00312                      RETURN_TRUE;
00313                      break;
00314 
00315               case IS_STRING:
00316                      if (is_numeric_string(Z_STRVAL_PP(arg), Z_STRLEN_PP(arg), NULL, NULL, 0)) {
00317                             RETURN_TRUE;
00318                      } else {
00319                             RETURN_FALSE;
00320                      }
00321                      break;
00322 
00323               default:
00324                      RETURN_FALSE;
00325                      break;
00326        }
00327 }
00328 /* }}} */
00329 
00330 /* {{{ proto bool is_scalar(mixed value)
00331    Returns true if value is a scalar */
00332 PHP_FUNCTION(is_scalar)
00333 {
00334        zval **arg;
00335 
00336        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
00337               return;
00338        }
00339 
00340        switch (Z_TYPE_PP(arg)) {
00341               case IS_BOOL:
00342               case IS_DOUBLE:
00343               case IS_LONG:
00344               case IS_STRING:
00345                      RETURN_TRUE;
00346                      break;
00347 
00348               default:
00349                      RETURN_FALSE;
00350                      break;
00351        }
00352 }
00353 /* }}} */
00354 
00355 /* {{{ proto bool is_callable(mixed var [, bool syntax_only [, string callable_name]]) 
00356    Returns true if var is callable. */
00357 PHP_FUNCTION(is_callable)
00358 {
00359        zval *var, **callable_name = NULL;
00360        char *name;
00361        char *error;
00362        zend_bool retval;
00363        zend_bool syntax_only = 0;
00364        int check_flags = 0;
00365 
00366        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|bZ", &var,
00367                                                    &syntax_only, &callable_name) == FAILURE) {
00368               return;
00369        }
00370        
00371        if (syntax_only) {
00372               check_flags |= IS_CALLABLE_CHECK_SYNTAX_ONLY;
00373        }
00374        if (ZEND_NUM_ARGS() > 2) {
00375               retval = zend_is_callable_ex(var, NULL, check_flags, &name, NULL, NULL, &error TSRMLS_CC);
00376               zval_dtor(*callable_name);
00377               ZVAL_STRING(*callable_name, name, 0);
00378        } else {
00379               retval = zend_is_callable_ex(var, NULL, check_flags, NULL, NULL, NULL, &error TSRMLS_CC);
00380        }
00381        if (error) {
00382               /* ignore errors */
00383               efree(error);
00384        }
00385 
00386        RETURN_BOOL(retval);
00387 }
00388 /* }}} */
00389 
00390 /*
00391  * Local variables:
00392  * tab-width: 4
00393  * c-basic-offset: 4
00394  * End:
00395  * vim600: sw=4 ts=4 fdm=marker
00396  * vim<600: sw=4 ts=4
00397  */