📄 var.c
字号:
/* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2007 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Jani Lehtim鋕i <jkl@njet.net> | | Thies C. Arntzen <thies@thieso.net> | | Sascha Schumann <sascha@schumann.cx> | +----------------------------------------------------------------------+*//* $Id: var.c,v 1.150.2.17.2.2 2007/01/01 09:46:49 sebastian Exp $ *//* {{{ includes */#include <stdio.h>#include <stdlib.h>#include <errno.h>#include "php.h"#include "php_string.h"#include "php_var.h"#include "php_smart_str.h"#include "basic_functions.h"#include "php_incomplete_class.h"#define COMMON ((*struc)->is_ref ? "&" : "")#define Z_REFCOUNT_PP(a) ((*a)->refcount)/* }}} *//* {{{ php_var_dump */static int php_array_element_dump(zval **zv, int num_args, va_list args, zend_hash_key *hash_key){ int level; TSRMLS_FETCH(); level = va_arg(args, int); if (hash_key->nKeyLength==0) { /* numeric key */ php_printf("%*c[%ld]=>\n", level + 1, ' ', hash_key->h); } else { /* string key */ php_printf("%*c[\"", level + 1, ' '); PHPWRITE(hash_key->arKey, hash_key->nKeyLength - 1); php_printf("\"]=>\n"); } php_var_dump(zv, level + 2 TSRMLS_CC); return 0;}PHPAPI void php_var_dump(zval **struc, int level TSRMLS_DC){ HashTable *myht = NULL; zend_object *object = NULL; int (*php_element_dump_func)(zval**, int, va_list, zend_hash_key*); php_element_dump_func = php_array_element_dump; if (level > 1) { php_printf("%*c", level - 1, ' '); } switch (Z_TYPE_PP(struc)) { case IS_BOOL: php_printf("%sbool(%s)\n", COMMON, Z_LVAL_PP(struc)?"true":"false"); break; case IS_NULL: php_printf("%sNULL\n", COMMON); break; case IS_LONG: php_printf("%sint(%ld)\n", COMMON, Z_LVAL_PP(struc)); break; case IS_DOUBLE: php_printf("%sfloat(%.*G)\n", COMMON, (int) EG(precision), Z_DVAL_PP(struc)); break; case IS_STRING: php_printf("%sstring(%d) \"", COMMON, Z_STRLEN_PP(struc)); PHPWRITE(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc)); PUTS("\"\n"); break; case IS_ARRAY: myht = Z_ARRVAL_PP(struc); if (myht->nApplyCount > 1) { PUTS("*RECURSION*\n"); return; } php_printf("%sarray(%d) {\n", COMMON, zend_hash_num_elements(myht)); php_element_dump_func = php_array_element_dump; goto head_done; case IS_OBJECT: object = Z_OBJ_PP(struc); myht = Z_OBJPROP_PP(struc); if (myht && myht->nApplyCount > 1) { PUTS("*RECURSION*\n"); return; } php_printf("%sobject(%s)(%d) {\n", COMMON, Z_OBJCE_PP(struc)->name, zend_hash_num_elements(myht));head_done: if (myht) { zend_hash_apply_with_arguments(myht, (apply_func_args_t) php_element_dump_func, 1, level); } if (level > 1) { php_printf("%*c", level-1, ' '); } PUTS("}\n"); break; case IS_RESOURCE: { char *type_name; type_name = zend_rsrc_list_get_rsrc_type(Z_LVAL_PP(struc) TSRMLS_CC); php_printf("%sresource(%ld) of type (%s)\n", COMMON, Z_LVAL_PP(struc), type_name ? type_name : "Unknown"); break; } default: php_printf("%sUNKNOWN:0\n", COMMON); break; }}/* }}} *//* {{{ proto void var_dump(mixed var) Dumps a string representation of variable to output */PHP_FUNCTION(var_dump){ zval ***args; int argc; int i; argc = ZEND_NUM_ARGS(); args = (zval ***)safe_emalloc(argc, sizeof(zval **), 0); if (ZEND_NUM_ARGS() == 0 || zend_get_parameters_array_ex(argc, args) == FAILURE) { efree(args); WRONG_PARAM_COUNT; } for (i=0; i<argc; i++) php_var_dump(args[i], 1 TSRMLS_CC); efree(args);}/* }}} *//* {{{ debug_zval_dump */static int zval_array_element_dump(zval **zv, int num_args, va_list args, zend_hash_key *hash_key){ int level; TSRMLS_FETCH(); level = va_arg(args, int); if (hash_key->nKeyLength==0) { /* numeric key */ php_printf("%*c[%ld]=>\n", level + 1, ' ', hash_key->h); } else { /* string key */ php_printf("%*c[\"", level + 1, ' '); PHPWRITE(hash_key->arKey, hash_key->nKeyLength - 1); php_printf("\"]=>\n"); } php_debug_zval_dump(zv, level + 2 TSRMLS_CC); return 0;}PHPAPI void php_debug_zval_dump(zval **struc, int level TSRMLS_DC){ HashTable *myht = NULL; if (level > 1) { php_printf("%*c", level - 1, ' '); } switch (Z_TYPE_PP(struc)) { case IS_BOOL: php_printf("%sbool(%s) refcount(%u)\n", COMMON, Z_LVAL_PP(struc)?"true":"false", Z_REFCOUNT_PP(struc)); break; case IS_NULL: php_printf("%sNULL refcount(%u)\n", COMMON, Z_REFCOUNT_PP(struc)); break; case IS_LONG: php_printf("%slong(%ld) refcount(%u)\n", COMMON, Z_LVAL_PP(struc), Z_REFCOUNT_PP(struc)); break; case IS_DOUBLE: php_printf("%sdouble(%.*G) refcount(%u)\n", COMMON, (int) EG(precision), Z_DVAL_PP(struc), Z_REFCOUNT_PP(struc)); break; case IS_STRING: php_printf("%sstring(%d) \"", COMMON, Z_STRLEN_PP(struc)); PHPWRITE(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc)); php_printf("\" refcount(%u)\n", Z_REFCOUNT_PP(struc)); break; case IS_ARRAY: myht = Z_ARRVAL_PP(struc); php_printf("%sarray(%d) refcount(%u){\n", COMMON, zend_hash_num_elements(myht), Z_REFCOUNT_PP(struc)); goto head_done; case IS_OBJECT: myht = Z_OBJPROP_PP(struc); php_printf("%sobject(%s)(%d) refcount(%u){\n", COMMON, Z_OBJCE_PP(struc)->name, zend_hash_num_elements(myht), Z_REFCOUNT_PP(struc));head_done: if (myht) { zend_hash_apply_with_arguments(myht, (apply_func_args_t) zval_array_element_dump, 1, level); } if (level > 1) { php_printf("%*c", level-1, ' '); } PUTS("}\n"); break; case IS_RESOURCE: { char *type_name; type_name = zend_rsrc_list_get_rsrc_type(Z_LVAL_PP(struc) TSRMLS_CC); php_printf("%sresource(%ld) of type (%s) refcount(%u)\n", COMMON, Z_LVAL_PP(struc), type_name ? type_name : "Unknown", Z_REFCOUNT_PP(struc)); break; } default: php_printf("%sUNKNOWN:0\n", COMMON); break; }}/* }}} *//* {{{ proto void debug_zval_dump(mixed var) Dumps a string representation of an internal zend value to output. */PHP_FUNCTION(debug_zval_dump){ zval ***args; int argc; int i; argc = ZEND_NUM_ARGS(); args = (zval ***)safe_emalloc(argc, sizeof(zval **), 0); if (ZEND_NUM_ARGS() == 0 || zend_get_parameters_array_ex(argc, args) == FAILURE) { efree(args); WRONG_PARAM_COUNT; } for (i=0; i<argc; i++) php_debug_zval_dump(args[i], 1 TSRMLS_CC); efree(args);}/* }}} *//* {{{ php_var_export */static int php_array_element_export(zval **zv, int num_args, va_list args, zend_hash_key *hash_key){ int level; TSRMLS_FETCH(); level = va_arg(args, int); if (hash_key->nKeyLength==0) { /* numeric key */ php_printf("%*c%ld => ", level + 1, ' ', hash_key->h); } else { /* string key */ char *key; int key_len; key = php_addcslashes(hash_key->arKey, hash_key->nKeyLength - 1, &key_len, 0, "'\\", 2 TSRMLS_CC); php_printf("%*c'", level + 1, ' '); PHPWRITE(key, key_len); php_printf("' => "); efree(key); } php_var_export(zv, level + 2 TSRMLS_CC); PUTS (",\n"); return 0;}static int php_object_element_export(zval **zv, int num_args, va_list args, zend_hash_key *hash_key){ int level; TSRMLS_FETCH(); level = va_arg(args, int); if (hash_key->nKeyLength != 0) { php_printf("%*cvar $%s = ", level + 1, ' ', hash_key->arKey); php_var_export(zv, level + 2 TSRMLS_CC); PUTS (";\n"); } return 0;}PHPAPI void php_var_export(zval **struc, int level TSRMLS_DC){ HashTable *myht; char* tmp_str; int tmp_len; switch (Z_TYPE_PP(struc)) { case IS_BOOL: php_printf("%s", Z_LVAL_PP(struc) ? "true" : "false"); break; case IS_NULL: php_printf("NULL"); break; case IS_LONG: php_printf("%ld", Z_LVAL_PP(struc)); break; case IS_DOUBLE: php_printf("%.*G", (int) EG(precision), Z_DVAL_PP(struc)); break; case IS_STRING: tmp_str = php_addcslashes(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc), &tmp_len, 0, "'\\", 2 TSRMLS_CC); PUTS ("'"); PHPWRITE(tmp_str, tmp_len); PUTS ("'"); efree (tmp_str); break; case IS_ARRAY: myht = Z_ARRVAL_PP(struc); if (level > 1) { php_printf("\n%*c", level - 1, ' '); } PUTS ("array (\n"); zend_hash_apply_with_arguments(myht, (apply_func_args_t) php_array_element_export, 1, level); if (level > 1) { php_printf("%*c", level - 1, ' '); } PUTS(")"); break; case IS_OBJECT: myht = Z_OBJPROP_PP(struc); if (level > 1) { php_printf("\n%*c", level - 1, ' '); } php_printf ("class %s {\n", Z_OBJCE_PP(struc)->name); if (myht) { zend_hash_apply_with_arguments(myht, (apply_func_args_t) php_object_element_export, 1, level); } if (level > 1) { php_printf("%*c", level - 1, ' '); } PUTS("}"); break; default: PUTS ("NULL"); break; }}/* }}} *//* {{{ proto mixed var_export(mixed var [, bool return]) Outputs or returns a string representation of a variable */PHP_FUNCTION(var_export){ zval *var; zend_bool return_output = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &var, &return_output) == FAILURE) { return; } if (return_output) { php_start_ob_buffer (NULL, 0, 1 TSRMLS_CC); } php_var_export(&var, 1 TSRMLS_CC);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -