zoukankan      html  css  js  c++  java
  • php dump 当前所有局部变量

    虽然有xdebug,平时还是用log或是var_dump调试的时候居多。

    经常想知道执行某个点时当前的变量情况,今天回来找了下参考php官网上 kailashbadu 的代码写了一个。

    核心在于 get_defined_vars() 函数。另外也有取出所有常量的get_defined_constants() 函数。

    /**
     * @desc show the variable that are to be excluded from the list.
     * $varList    array
     * $excludeList    array
     */
    function logVars($varList, $excludeList = null) {
        if ($excludeList == null)
            $excludeList = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET');
    
        $list = array();
        foreach ($varList as $key => $value) {
            if (!in_array($key, $excludeList))
                $list[$key] = $value;
        }
    
        echo '<pre>';
        print_r($list);
        echo '</pre>';
    }
    
    //some dummy variables; add your own or include a file.
    $firstName = 'kailash';
    $lastName = 'Badu';
    $test = array('Pratistha', 'sanu', 'fuchhi');
    
    
    logVars(get_defined_vars());
    
    // results
    /*
    Array
    (
        [firstName] => kailash
        [lastName] => Badu
        [test] => Array
            (
                [0] => Pratistha
                [1] => sanu
                [2] => fuchhi
            )
    )
    */
    
    

    参考: http://cn.php.net/manual/en/function.get-defined-vars.php

  • 相关阅读:
    构建之法阅读笔记05
    构建之法阅读笔记04
    构建之法阅读笔记03
    学习进度条
    软件工程练习——买书
    软件工程练习——找水王2
    Java作业07
    Java课堂作业06
    读《大道至简》第六章有感
    Java课堂作业05
  • 原文地址:https://www.cnblogs.com/nonlyli/p/1697333.html
Copyright © 2011-2022 走看看