var_dump()和print_r()在输出字符串数组方面有什么区别?


当前回答

我还建议在输出到浏览器时,将var_dump()或printr的输出放到pre标记中。

print "<pre>";
print_r($dataset);
print "</pre>";

将给出更可读的结果。

其他回答

如果您问什么时候应该使用什么,我通常使用print_r()来显示值,使用var_dump()来处理变量类型的问题。

对于所有需要var_dump()函数进行测试的人!

I've written my own test dump function, cause i didn't whant to do that ob_start ... stuff over and over again. The nice thing, it even get's the parameter names from source!
function test_dump (...$params)
{
    
    $file_test_dump = 'test_dump.log';

    $backtrace = debug_backtrace(0, 3);

    $caller_file        = $backtrace[0]['file']);  
    $caller_function    = $backtrace[1]['function'];
    $caller_line        = $backtrace[0]['line'];

    if (empty($params))
    {
        file_put_contents ($file_test_dump, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" , FILE_APPEND);
        file_put_contents ($file_test_dump, '!!!!!! ' . date ("[Y-m-d H:i:s:u]", time()) . " - FILE: {$caller_file} | FUNCTION: {$caller_function} | LINE: {$caller_line} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n\n" , FILE_APPEND);
        file_put_contents ($file_test_dump, "!!! No parameter given !!! \n\n" , FILE_APPEND);
        return;
    }
    
    

    // --- get source file ---
    $source_file = file($backtrace[0]['file']);
    
    // --- get line of source file and pre filter with regex ---
    preg_match ('~\(([^*]*)\)~', $source_file [intval($backtrace[0]['line']) - 1], $parameter_line);
    $parameter_line_filtered = str_replace(" ", "", $parameter_line [1]);

    // put parameter names into array
    $params_names = explode(',', $parameter_line_filtered);



    // --- OUTPUT   ----
    
    file_put_contents ($file_test_dump, "#######################################################################################################################################################################################################################\n" , FILE_APPEND);
    file_put_contents ($file_test_dump, '### ' . date ("[Y-m-d H:i:s:u]", time()) . " - FILE: {$caller_file} | FUNCTION: {$caller_function} | LINE: {$caller_line} ### \n\n" , FILE_APPEND);
    
    
    $i = 0;
    
    foreach ($params as $param)
    {

        $i++;

        file_put_contents ($file_test_dump, "   --- Parameter " . $i . ': ' . $params_names[$i - 1] . " --------------------------------------------------------------------------------------------------------------------------\n", FILE_APPEND);

        if (    is_array($param)    ||
                is_object($param) ||
                is_bool($param) )
        {
            ob_start();
            var_dump($param);
            file_put_contents ($file_test_dump, ob_get_contents() . "\n\n", FILE_APPEND);
            ob_end_clean();
        }
        else
        {
            file_put_contents ($file_test_dump, $param . "\n\n", FILE_APPEND);
        }


    }

    file_put_contents ($file_test_dump, "\n\n", FILE_APPEND);

}

Var_dump()将向您显示对象的类型以及其中的内容。

你会得到=> (string)示例如下。

Print_r()只输出内容。

示例如下:

Var_dump ($var)通过提供

值的数据类型(包括后代元素) 变量中的元素数 值的长度

对于大型数组,print_r显示的信息可能远远超出有用的范围。您可以像这样截断它,显示前2000个字符或任何您需要的字符。

  echo "<pre>" . substr(print_r($dataset, 1), 0, 2000) . "</pre>";