{"id":786,"date":"2017-03-18T15:00:01","date_gmt":"2017-03-18T09:30:01","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=786"},"modified":"2017-03-29T15:00:10","modified_gmt":"2017-03-29T09:30:10","slug":"can-capture-result-var_dump-string-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/can-capture-result-var_dump-string-2\/","title":{"rendered":"PHP &#8211; How can we capture the result of var_dump to a string"},"content":{"rendered":"<p><strong>What is var_dump?<\/strong><\/p>\n<p>The\u00a0var_dump manual page [<a href=\"http:\/\/php.net\/var_dump\"> http:\/\/php.net\/var_dump<\/a> ]\u00a0states that var_dump displays structured information about one or more expressions that includes its type and value.<\/p>\n<p>Arrays and objects are explored recursively with values indented to show structure.<\/p>\n<p><strong>Description<\/strong> :<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dvoid%20var_dump%20(%20mixed%20%24expression%20%5B%2C%20mixed%20%24\u2026%20%5D%20)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>This function displays structured information about one or more expressions that includes its type and value.<\/p>\n<p>Arrays and objects are explored recursively with values indented to show structure.<\/p>\n<p>All public, private and protected properties of objects will be returned in the output unless the object implements a\u00a0__debugInfo()<br \/>\n<strong>[ http:\/\/us3.php.net\/manual\/en\/language.oop5.magic.php#language.oop5.magic.debuginfo ]\u00a0<\/strong>method (implemented in PHP 5.6.0).<\/p>\n<p><strong>Example #1<\/strong><\/p>\n<p><strong>var_dump() example:<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%0A%24b%20%3D%203.1%3B%0A%24c%20%3D%20true%3B%0Avar_dump(%24b%2C%20%24c)%3B%0A%0A%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>The above example will output:<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dfloat(3.1)%0Abool(true)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>The problem is that var_dump outputs its result directly to the browser, how can you capture its output in a string variable? read on\u2026<\/p>\n<p><strong>Using output control functions for the solution:<\/strong><\/p>\n<p>Output control functions can be used to capture and redirect the standard output. For more details, read the\u00a0PHP manual on output control functions<br \/>\n<strong>[ http:\/\/php.net\/manual\/en\/ref.outcontrol.php ], <\/strong>of course.<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%0Afunction%20varDumpToString(%24var)%20%7B%0A%20%20%20%20ob_start()%3B%0A%20%20%20%20var_dump(%24var)%3B%0A%20%20%20%20%24result%20%3D%20ob_get_clean()%3B%0A%20%20%20%20return%20%24result%3B%0A%7D%0A%0A%2F%2F%0A%2F%2FExample%20usage%3A%0A%2F%2F%20%20%20%20%24data%20%3D%20array(\u2018first\u2019%2C%20\u2019second\u2019%2C%20\u2019third\u2019)%3B%0A%2F%2F%20%20%20%20%24result%20%3D%20varDumpToString(%24data)%3B%0A%2F%2F%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><strong>Sample code:<\/strong><\/p>\n<p><strong>Use output buffering:<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0Aob_start()%3B%0Avar_dump(%24someVar)%3B%0A%24result%20%3D%20ob_get_clean()%3B%0A%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>A smart alternative to PHP\u2019s \u201cvar_dump\u201d function<\/strong><\/p>\n<p>The \u201cvar_debug\u201d function is a smart alternative to PHP\u2019s \u201cvar_dump\u201d function, which limits the output and prints the file path and line of the invocation.<\/p>\n<p><strong>File path and line of invocation<\/strong><\/p>\n<p>The \u201cvar_debug\u201d function outputs the file path and line number of the invocation. So even when your \u201cvar_dump\u201d calls all say the same you can still see what happened. To accomplish this, it uses the PHP \u201cdebug_backtrace\u201d function.<\/p>\n<p>It sets the \u201cDEBUG_BACKTRACE_IGNORE_ARGS\u201d option to reduce memory usage. From the produced backtrace, it gets the first element that holds a file path and line number and it adds that to the start of the output.<\/p>\n<p><strong>Limit the output:<\/strong><\/p>\n<p>The \u201cvar_debug\u201d function limits the output with the following default rules:<\/p>\n<p>Only the first 100 characters of each string are logged (full length is shown)<\/p>\n<p>Only the first 25 elements of an array are logged (full length is shown)<\/p>\n<p>Only the first 10 levels of nested objects\/arrays are logged<\/p>\n<p>These three limits can be set using three optional parameters in the above order. Hence, calling \u201cvar_debug($variable)\u201d is equal to calling \u201cvar_debug($variable,100,25,10)\u201d.<\/p>\n<p><strong>Modifications:<\/strong><\/p>\n<p><strong>If you would like the function to echo HTML-printable output, you can replace the last line that says \u201cecho $string;\u201d with the following:<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201decho%20nl2br(str_replace(\u2018%20\u2019%2C\u2019%26nbsp%3B\u2019%2Chtmlentities(%24string)))%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Replace \u201cecho $string;\u201d with \u201creturn $string;\u201d if you want to use the output for logging purposes in some other function.<\/p>\n<p><strong>var_export<\/strong><\/p>\n<p><strong>You may want to check out var_export \u2014 while it doesn\u2019t provide the same output as var_dump it does provide a second $return parameter which will cause it to return its output rather than print it:<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24debug%20%3D%20var_export(%24my_var%2C%20true)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>The difference between var_dump and var_export is that var_export returns a \u201cparsable string representation of a variable\u201d while var_dump simply dumps information about a variable.<\/p>\n<p>What this means in practice is that var_export gives you valid PHP code.<\/p>\n<p><strong>Demo:<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24demo%20%3D%20array(%0A%20%20%20%20%22bool%22%20%3D%3E%20false%2C%0A%20%20%20%20%22int%22%20%3D%3E%201%2C%0A%20%20%20%20%22float%22%20%3D%3E%203.14%2C%0A%20%20%20%20%22string%22%20%3D%3E%20%22hello%20world%22%2C%0A%20%20%20%20%22array%22%20%3D%3E%20array()%2C%0A%20%20%20%20%22object%22%20%3D%3E%20new%20stdClass()%2C%0A%20%20%20%20%22resource%22%20%3D%3E%20tmpfile()%2C%0A%20%20%20%20%22null%22%20%3D%3E%20null%2C%0A)%3B%0A%0A%2F%2F%20var_export%20\u2013%20nice%2C%20one-liner%0A%24debug_export%20%3D%20var_export(%24demo%2C%20true)%3B%0A%0A%2F%2F%20var_dump%0Aob_start()%3B%0Avar_dump(%24demo)%3B%0A%24debug_dump%20%3D%20ob_get_clean()%3B%0A%0A%2F%2F%20print_r%20\u2013%20included%20for%20completeness%2C%20though%20not%20recommended%0A%24debug_printr%20%3D%20print_r(%24demo%2C%20true)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>The difference in output:<\/strong><\/p>\n<p><strong>var_export ($debug_export in above example)<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201darray%20(%0A%20%20\u2019bool\u2019%20%3D%3E%20false%2C%0A%20%20\u2019int\u2019%20%3D%3E%201%2C%0A%20%20\u2019float\u2019%20%3D%3E%203.1400000000000001%2C%0A%20%20\u2019string\u2019%20%3D%3E%20\u2019hello%20world\u2019%2C%0A%20%20\u2019array\u2019%20%3D%3E%20%0A%20%20array%20(%0A%20%20)%2C%0A%20%20\u2019object\u2019%20%3D%3E%20%0A%20%20stdClass%3A%3A__set_state(array(%0A%20%20))%2C%0A%20%20\u2019resource\u2019%20%3D%3E%20NULL%2C%20%2F%2F%20Note%20that%20this%20resource%20pointer%20is%20now%20NULL%0A%20%20\u2019null\u2019%20%3D%3E%20NULL%2C%0A)%3A%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>var_dump ($debug_dump in above example)<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201darray(8)%20%7B%0A%20%20%5B%22bool%22%5D%3D%3E%0A%20%20bool(false)%0A%20%20%5B%22int%22%5D%3D%3E%0A%20%20int(1)%0A%20%20%5B%22float%22%5D%3D%3E%0A%20%20float(3.14)%0A%20%20%5B%22string%22%5D%3D%3E%0A%20%20string(11)%20%22hello%20world%22%0A%20%20%5B%22array%22%5D%3D%3E%0A%20%20array(0)%20%7B%0A%20%20%7D%0A%20%20%5B%22object%22%5D%3D%3E%0A%20%20object(stdClass)%231%20(0)%20%7B%0A%20%20%7D%0A%20%20%5B%22resource%22%5D%3D%3E%0A%20%20resource(4)%20of%20type%20(stream)%0A%20%20%5B%22null%22%5D%3D%3E%0A%20%20NULL%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>print_r ($debug_printr in above example):<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dArray%0A(%0A%20%20%20%20%5Bbool%5D%20%3D%3E%20%0A%20%20%20%20%5Bint%5D%20%3D%3E%201%0A%20%20%20%20%5Bfloat%5D%20%3D%3E%203.14%0A%20%20%20%20%5Bstring%5D%20%3D%3E%20hello%20world%0A%20%20%20%20%5Barray%5D%20%3D%3E%20Array%0A%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%5Bobject%5D%20%3D%3E%20stdClass%20Object%0A%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%5Bresource%5D%20%3D%3E%20Resource%20id%20%234%0A%20%20%20%20%5Bnull%5D%20%3D%3E%20%0A)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Caveat: var_export does not handle circular references<\/strong><\/p>\n<p><strong>If you\u2019re trying to dump a variable with circular references, calling var_export will result in a PHP warning:<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24circular%20%3D%20array()%3B%0A%20%24circular%5B\u2019self\u2019%5D%20%3D%26%20%24circular%3B%0A%20var_export(%24circular)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Results in:<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dWarning%3A%20var_export%20does%20not%20handle%20circular%20references%20in%20example.php%20on%20line%203%0A%20array%20(%0A%20%20%20\u2019self\u2019%20%3D%3E%20%0A%20%20%20array%20(%0A%20%20%20%20%20\u2019self\u2019%20%3D%3E%20NULL%2C%0A%20%20%20)%2C%0A%20)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>Both var_dump and print_r, on the other hand, will output the string *RECURSION* when encountering circular references.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is var_dump? The\u00a0var_dump manual page [ http:\/\/php.net\/var_dump ]\u00a0states that var_dump displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure. Description : [pastacode lang=\u201dphp\u201d manual=\u201dvoid%20var_dump%20(%20mixed%20%24expression%20%5B%2C%20mixed%20%24\u2026%20%5D%20)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] [ad type=\u201dbanner\u201d] This function displays structured information about one [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[1428,1436,1432,1435,1431,1434,1426,1429,1433,1430,1427],"class_list":["post-786","post","type-post","status-publish","format-standard","hentry","category-php","tag-how-to-put-php-var_dump-in-a-variable","tag-how-to-remove-a-particular-element-from-an-array-in-javascript","tag-php-var_dump-all-variables","tag-php-var_dump-to-error-log","tag-php-var_export","tag-print_r-to-string","tag-the-definitive-guide-to-phps-isset-and-empty","tag-var_dump-vs-print_r","tag-var_dump_post","tag-var_export-to-file","tag-writing-to-the-php-error_log-with-var_dump-print_r"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=786"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/786\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}