php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
type.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Rasmus Lerdorf <rasmus@php.net> |
14 +----------------------------------------------------------------------+
15*/
16
17#include "php.h"
18
19/* {{{ Returns the type of the variable */
36/* }}} */
37
38/* {{{ Returns the type of the variable resolving class names */
40{
41 zval *arg;
42 const char *name;
43
47
48 switch (Z_TYPE_P(arg)) {
49 case IS_NULL:
50 RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE));
51 case IS_FALSE:
52 case IS_TRUE:
53 RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_BOOL));
54 case IS_LONG:
55 RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_INT));
56 case IS_DOUBLE:
57 RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_FLOAT));
58 case IS_STRING:
59 RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_STRING));
60 case IS_ARRAY:
61 RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_ARRAY));
62 case IS_OBJECT:
63 if (Z_OBJ_P(arg)->ce->ce_flags & ZEND_ACC_ANON_CLASS) {
64 name = ZSTR_VAL(Z_OBJ_P(arg)->ce->name);
65 RETURN_NEW_STR(zend_string_init(name, strlen(name), 0));
66 } else {
67 RETURN_STR_COPY(Z_OBJ_P(arg)->ce->name);
68 }
69 case IS_RESOURCE:
71 if (name) {
72 RETURN_NEW_STR(zend_strpprintf(0, "resource (%s)", name));
73 } else {
74 RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_CLOSED_RESOURCE));
75 }
76 default:
77 RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_UNKNOWN));
78 }
79}
80/* }}} */
81
82
83/* {{{ Set the type of the variable */
85{
86 zval *var;
88 zval tmp, *ptr;
89
91 Z_PARAM_ZVAL(var)
94
97 ZVAL_COPY(&tmp, Z_REFVAL_P(var));
98 ptr = &tmp;
99 } else {
100 ptr = Z_REFVAL_P(var);
101 }
102 if (zend_string_equals_ci(type, ZSTR_KNOWN(ZEND_STR_INTEGER))) {
104 } else if (zend_string_equals_ci(type, ZSTR_KNOWN(ZEND_STR_INT))) {
106 } else if (zend_string_equals_ci(type, ZSTR_KNOWN(ZEND_STR_FLOAT))) {
108 } else if (zend_string_equals_ci(type, ZSTR_KNOWN(ZEND_STR_DOUBLE))) { /* deprecated */
110 } else if (zend_string_equals_ci(type, ZSTR_KNOWN(ZEND_STR_STRING))) {
112 } else if (zend_string_equals_ci(type, ZSTR_KNOWN(ZEND_STR_ARRAY))) {
114 } else if (zend_string_equals_ci(type, ZSTR_KNOWN(ZEND_STR_OBJECT))) {
116 } else if (zend_string_equals_ci(type, ZSTR_KNOWN(ZEND_STR_BOOL))) {
118 } else if (zend_string_equals_ci(type, ZSTR_KNOWN(ZEND_STR_BOOLEAN))) {
120 } else if (zend_string_equals_ci(type, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE))) {
122 } else {
123 if (ptr == &tmp) {
124 zval_ptr_dtor(&tmp);
125 }
126 if (zend_string_equals_ci(type, ZSTR_KNOWN(ZEND_STR_RESOURCE))) {
127 zend_value_error("Cannot convert to resource type");
128 } else {
129 zend_argument_value_error(2, "must be a valid type");
130 }
132 }
133
134 if (ptr == &tmp) {
136 }
138}
139/* }}} */
140
141/* {{{ Get the integer value of a variable using the optional base for the conversion */
143{
144 zval *num;
145 zend_long base = 10;
146
148 Z_PARAM_ZVAL(num)
150 Z_PARAM_LONG(base)
152
153 if (Z_TYPE_P(num) != IS_STRING || base == 10) {
154 RETVAL_LONG(zval_get_long(num));
155 return;
156 }
157
158
159 if (base == 0 || base == 2) {
160 char *strval = Z_STRVAL_P(num);
161 size_t strlen = Z_STRLEN_P(num);
162
163 while (isspace(*strval) && strlen) {
164 strval++;
165 strlen--;
166 }
167
168 /* Length of 3+ covers "0b#" and "-0b" (which results in 0) */
169 if (strlen > 2) {
170 int offset = 0;
171 if (strval[0] == '-' || strval[0] == '+') {
172 offset = 1;
173 }
174
175 if (strval[offset] == '0' && (strval[offset + 1] == 'b' || strval[offset + 1] == 'B')) {
176 char *tmpval;
177 strlen -= 2; /* Removing "0b" */
178 tmpval = emalloc(strlen + 1);
179
180 /* Place the unary symbol at pos 0 if there was one */
181 if (offset) {
182 tmpval[0] = strval[0];
183 }
184
185 /* Copy the data from after "0b" to the end of the buffer */
186 memcpy(tmpval + offset, strval + offset + 2, strlen - offset);
187 tmpval[strlen] = 0;
188
189 RETVAL_LONG(ZEND_STRTOL(tmpval, NULL, 2));
190 efree(tmpval);
191 return;
192 }
193 }
194 }
195
197}
198/* }}} */
199
200/* {{{ Get the float value of a variable */
202{
203 zval *num;
204
206 Z_PARAM_ZVAL(num)
208
209 RETURN_DOUBLE(zval_get_double(num));
210}
211/* }}} */
212
213/* {{{ Get the boolean value of a variable */
224/* }}} */
225
226/* {{{ Get the string value of a variable */
228{
229 zval *value;
230
234
235 RETVAL_STR(zval_get_string(value));
236}
237/* }}} */
238
239static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type)
240{
241 zval *arg;
242
246
247 if (Z_TYPE_P(arg) == type) {
248 if (type == IS_RESOURCE) {
249 const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg));
250 if (!type_name) {
252 }
253 }
255 } else {
257 }
258}
259
260
261/* {{{ Returns true if variable is null
262 Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
267/* }}} */
268
269/* {{{ Returns true if variable is a resource
270 Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
275/* }}} */
276
277/* {{{ Returns true if variable is a boolean
278 Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
289/* }}} */
290
291/* {{{ Returns true if variable is an integer
292 Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
297/* }}} */
298
299/* {{{ Returns true if variable is float point
300 Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
305/* }}} */
306
307/* {{{ Returns true if variable is a string
308 Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
313/* }}} */
314
315/* {{{ Returns true if variable is an array
316 Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
321/* }}} */
322
323/* {{{ Returns true if $array is an array whose keys are all numeric, sequential, and start at 0 */
325{
326 HashTable *array;
327
329 Z_PARAM_ARRAY_HT(array)
331
332 RETURN_BOOL(zend_array_is_list(array));
333}
334/* }}} */
335
336/* {{{ Returns true if variable is an object
337 Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
342/* }}} */
343
344static inline void _zend_is_numeric(zval *return_value, zval *arg)
345{
346 switch (Z_TYPE_P(arg)) {
347 case IS_LONG:
348 case IS_DOUBLE:
350 break;
351
352 case IS_STRING:
353 if (is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 0)) {
355 } else {
357 }
358 break;
359
360 default:
362 break;
363 }
364}
365
366/* {{{ Returns true if value is a number or a numeric string */
368{
369 zval *arg;
370
374
375 _zend_is_numeric(return_value, arg);
376}
377/* }}} */
378
380{
381 zval *arg;
382
384
385 _zend_is_numeric(return_value, arg);
386}
387
388/* {{{ Returns true if value is a scalar */
390{
391 zval *arg;
392
396
397 switch (Z_TYPE_P(arg)) {
398 case IS_FALSE:
399 case IS_TRUE:
400 case IS_DOUBLE:
401 case IS_LONG:
402 case IS_STRING:
404 break;
405
406 default:
408 break;
409 }
410}
411/* }}} */
412
413/* {{{ Returns true if var is callable. */
415{
416 zval *var, *callable_name = NULL;
418 bool retval;
419 bool syntax_only = 0;
420 int check_flags = 0;
421
423 Z_PARAM_ZVAL(var)
425 Z_PARAM_BOOL(syntax_only)
426 Z_PARAM_ZVAL(callable_name)
428
429 if (syntax_only) {
430 check_flags |= IS_CALLABLE_CHECK_SYNTAX_ONLY;
431 }
432 if (ZEND_NUM_ARGS() > 2) {
433 retval = zend_is_callable_ex(var, NULL, check_flags, &name, NULL, NULL);
434 ZEND_TRY_ASSIGN_REF_STR(callable_name, name);
435 } else {
436 retval = zend_is_callable_ex(var, NULL, check_flags, NULL, NULL, NULL);
437 }
438
440}
441/* }}} */
442
443/* {{{ Returns true if var is iterable (array or instance of Traversable). */
454/* }}} */
455
456/* {{{ Returns true if var is countable (array or instance of Countable). */
467/* }}} */
strval(mixed $value)
is_countable(mixed $value)
array_is_list(array $array)
settype(mixed &$var, string $type)
is_null(mixed $value)
boolval(mixed $value)
is_array(mixed $value)
is_callable(mixed $value, bool $syntax_only=false, &$callable_name=null)
intval(mixed $value, int $base=10)
is_float(mixed $value)
floatval(mixed $value)
gettype(mixed $value)
is_bool(mixed $value)
is_object(mixed $value)
is_scalar(mixed $value)
is_numeric(mixed $value)
is_resource(mixed $value)
is_int(mixed $value)
get_debug_type(mixed $value)
is_iterable(mixed $value)
is_string(mixed $value)
zend_ffi_type * type
Definition ffi.c:3812
void * ptr
Definition ffi.c:3814
memcpy(ptr1, ptr2, size)
zval * arg
Definition ffi.c:3975
zend_long offset
#define NULL
Definition gdcache.h:45
foreach($dp as $el) foreach( $dp as $el) if( $pass2< 2) echo ""
#define PHP_FUNCTION
Definition php.h:364
ZEND_API zend_string * zend_strpprintf(size_t max_len, const char *format,...)
Definition zend.c:353
ZEND_API ZEND_COLD void zend_value_error(const char *format,...)
Definition zend.c:1849
#define INTERNAL_FUNCTION_PARAMETERS
Definition zend.h:49
#define INTERNAL_FUNCTION_PARAM_PASSTHRU
Definition zend.h:50
ZEND_API bool zend_is_countable(const zval *countable)
Definition zend_API.c:5287
ZEND_API bool zend_is_callable_ex(zval *callable, zend_object *object, uint32_t check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error)
Definition zend_API.c:4271
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:433
ZEND_API zend_string * zend_zval_get_legacy_type(const zval *arg)
Definition zend_API.c:184
ZEND_API zend_result zend_try_assign_typed_ref(zend_reference *ref, zval *val)
Definition zend_API.c:4677
ZEND_API bool zend_is_iterable(const zval *iterable)
Definition zend_API.c:5274
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define RETURN_STRING(s)
Definition zend_API.h:1043
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define RETURN_FALSE
Definition zend_API.h:1058
#define RETURN_DOUBLE(d)
Definition zend_API.h:1038
#define Z_PARAM_OPTIONAL
Definition zend_API.h:1667
#define Z_PARAM_STR(dest)
Definition zend_API.h:2086
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define Z_PARAM_LONG(dest)
Definition zend_API.h:1896
#define RETURN_BOOL(b)
Definition zend_API.h:1035
#define RETURN_NEW_STR(s)
Definition zend_API.h:1041
#define RETURN_THROWS()
Definition zend_API.h:1060
#define RETVAL_TRUE
Definition zend_API.h:1033
#define Z_PARAM_ARRAY_HT(dest)
Definition zend_API.h:1852
#define RETVAL_LONG(l)
Definition zend_API.h:1011
#define Z_PARAM_BOOL(dest)
Definition zend_API.h:1726
#define RETURN_INTERNED_STR(s)
Definition zend_API.h:1040
#define Z_PARAM_ZVAL(dest)
Definition zend_API.h:2100
#define RETVAL_STR(s)
Definition zend_API.h:1013
#define ZEND_TRY_ASSIGN_REF_STR(zv, str)
Definition zend_API.h:1271
#define IS_CALLABLE_CHECK_SYNTAX_ONLY
Definition zend_API.h:411
#define RETURN_TRUE
Definition zend_API.h:1059
#define RETURN_STR_COPY(s)
Definition zend_API.h:1042
#define efree(ptr)
Definition zend_alloc.h:155
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
strlen(string $string)
#define ZEND_ACC_ANON_CLASS
#define ZEND_REF_HAS_TYPE_SOURCES(ref)
#define Z_FLF_PARAM_ZVAL(arg_num, dest)
#define ZEND_FRAMELESS_FUNCTION(name, arity)
const char * zend_rsrc_list_get_rsrc_type(zend_resource *res)
Definition zend_list.c:316
int32_t zend_long
Definition zend_long.h:42
#define ZEND_STRTOL(s0, s1, base)
Definition zend_long.h:85
struct _zend_string zend_string
ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op)
ZEND_API void ZEND_FASTCALL convert_to_array(zval *op)
ZEND_API void ZEND_FASTCALL convert_to_double(zval *op)
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op)
ZEND_API void ZEND_FASTCALL convert_to_object(zval *op)
ZEND_API void ZEND_FASTCALL convert_to_null(zval *op)
ZEND_API void ZEND_FASTCALL convert_to_long(zval *op)
#define convert_to_string(op)
#define EXPECTED(condition)
#define ZEND_ASSERT(c)
#define UNEXPECTED(condition)
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_KNOWN(idx)
#define zend_string_equals_ci(s1, s2)
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define IS_TRUE
Definition zend_types.h:603
#define Z_ISREF_P(zval_p)
Definition zend_types.h:954
#define Z_REFVAL_P(zval_p)
#define IS_FALSE
Definition zend_types.h:602
#define Z_STRVAL_P(zval_p)
Definition zend_types.h:975
#define IS_STRING
Definition zend_types.h:606
struct _zend_array HashTable
Definition zend_types.h:386
#define IS_RESOURCE
Definition zend_types.h:609
#define Z_OBJ_P(zval_p)
Definition zend_types.h:990
#define IS_ARRAY
Definition zend_types.h:607
#define IS_DOUBLE
Definition zend_types.h:605
#define Z_STRLEN_P(zval_p)
Definition zend_types.h:978
#define IS_NULL
Definition zend_types.h:601
#define IS_OBJECT
Definition zend_types.h:608
#define IS_LONG
Definition zend_types.h:604
#define ZVAL_COPY(z, v)
#define Z_REF_P(zval_p)
#define Z_RES_P(zval_p)
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
zval retval
zval * return_value
zend_string * name
value