]> granicus.if.org Git - php/commitdiff
- Added var_export, which shows a representation of a variable, much like
authorDerick Rethans <derick@php.net>
Sat, 8 Dec 2001 23:44:34 +0000 (23:44 +0000)
committerDerick Rethans <derick@php.net>
Sat, 8 Dec 2001 23:44:34 +0000 (23:44 +0000)
  var_dump, but in such a way you can use it as PHP code.
@- Added var_export, which shows a representation of a variable, much like
@  var_dump, but in such a way you can use it as PHP code. (Derick)

ext/standard/basic_functions.c
ext/standard/php_var.h
ext/standard/var.c

index ed7dbccc73ef6a06a3487f4736c11a7d97891a5c..2ecf5adf44b24e81e4745b51434e6c084cbe1964 100644 (file)
@@ -533,6 +533,7 @@ function_entry basic_functions[] = {
        PHP_FE(unserialize,                                                                                                             NULL)
 
        PHP_FE(var_dump,                                                                                                                NULL)
+       PHP_FE(var_export,                                                                                                              NULL)
        PHP_FE(print_r,                                                                                                                 NULL)
 
        PHP_FE(register_shutdown_function,                                                                              NULL)
index aa5e00395506caaaac17df8f44f86881e7563372..bcab5cfe735f21aa0488869de43936274b3d7602 100644 (file)
 #include "ext/standard/php_smart_str_public.h"
 
 PHP_FUNCTION(var_dump);
+PHP_FUNCTION(var_export);
 PHP_FUNCTION(serialize);
 PHP_FUNCTION(unserialize);
 
 void php_var_dump(zval **struc, int level TSRMLS_DC);
+void php_var_export(zval **struc, int level TSRMLS_DC);
 
 /* typdef HashTable php_serialize_data_t; */
 #define php_serialize_data_t HashTable
index a765abf975394f2fa19d0fe61e8cc7ae6d6ac214..b37f575161c1489f7f1bbde5e23f6ca49a6ae710 100644 (file)
@@ -136,6 +136,101 @@ PHP_FUNCTION(var_dump)
 /* }}} */
 
 
+/* {{{ 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 */
+               php_printf("%*c'%s' => ", level + 1, ' ', hash_key->arKey);
+       }
+       php_var_export(zv, level + 2 TSRMLS_CC);
+       PUTS (",\n");
+       return 0;
+}
+
+void php_var_export(zval **struc, int level TSRMLS_DC)
+{
+       HashTable *myht;
+/*
+       if (level > 1) {
+               php_printf("%*c", level - 1, ' ');
+       }
+*/
+       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:
+               PUTS ("'");
+               PHPWRITE(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc));
+               PUTS ("'");
+               break;
+       case IS_ARRAY:
+               myht = Z_ARRVAL_PP(struc);
+               goto head_done;
+       case IS_OBJECT:
+               myht = Z_OBJPROP_PP(struc);
+head_done:
+               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;
+       default:
+               PUTS ("NULL");
+               break;
+       }
+}
+
+/* }}} */
+
+
+/* {{{ proto void var_export(mixed var)
+   Dumps a string representation of variable to output */
+PHP_FUNCTION(var_export)
+{
+       zval ***args;
+       int argc;
+       int     i;
+       
+       argc = ZEND_NUM_ARGS();
+       
+       args = (zval ***)emalloc(argc * sizeof(zval **));
+       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_export(args[i], 1 TSRMLS_CC);
+       
+       efree(args);
+}
+/* }}} */
+
+
+
 /* {{{ php_var_serialize */
 
 static void php_var_serialize_intern(smart_str *buf, zval **struc, HashTable *var_hash TSRMLS_DC);