How can I capture the result of var_dump to a string?

后端 未结 13 1626
挽巷
挽巷 2020-11-27 08:39

I\'d like to capture the output of var_dump to a string.

The PHP documentation says;

As with anything that outputs its result directly to the

13条回答
  •  醉梦人生
    2020-11-27 09:22

    From the PHP manual:

    This function displays structured information about one or more expressions that includes its type and value.

    So, here is the real return version of PHP's var_dump(), which actually accepts a variable-length argument list:

    function var_dump_str()
    {
        $argc = func_num_args();
        $argv = func_get_args();
    
        if ($argc > 0) {
            ob_start();
            call_user_func_array('var_dump', $argv);
            $result = ob_get_contents();
            ob_end_clean();
            return $result;
        }
    
        return '';
    }
    

提交回复
热议问题