{"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\" target=\"_blank\" rel=\"noopener\"> 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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">void var_dump ( mixed $expression [, mixed $... ] )<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/><br\/>$b = 3.1;<br\/>$c = true;<br\/>var_dump($b, $c);<br\/><br\/>?&gt;<\/code><\/pre> <\/div>\n<p><strong>The above example will output:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">float(3.1)<br\/>bool(true)<\/code><\/pre> <\/div>\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&#8230;<\/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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/><br\/>function varDumpToString($var) {<br\/>    ob_start();<br\/>    var_dump($var);<br\/>    $result = ob_get_clean();<br\/>    return $result;<br\/>}<br\/><br\/>\/\/<br\/>\/\/Example usage:<br\/>\/\/    $data = array(&#039;first&#039;, &#039;second&#039;, &#039;third&#039;);<br\/>\/\/    $result = varDumpToString($data);<br\/>\/\/<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Sample code:<\/strong><\/p>\n<p><strong>Use output buffering:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/>ob_start();<br\/>var_dump($someVar);<br\/>$result = ob_get_clean();<br\/>?&gt;<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">echo nl2br(str_replace(&#039; &#039;,&#039;&nbsp;&#039;,htmlentities($string)));<\/code><\/pre> <\/div>\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&#8217;t 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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$debug = var_export($my_var, true);<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>The difference between var_dump and var_export is that var_export returns a &#8220;parsable string representation of a variable&#8221; 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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$demo = array(<br\/>    &quot;bool&quot; =&gt; false,<br\/>    &quot;int&quot; =&gt; 1,<br\/>    &quot;float&quot; =&gt; 3.14,<br\/>    &quot;string&quot; =&gt; &quot;hello world&quot;,<br\/>    &quot;array&quot; =&gt; array(),<br\/>    &quot;object&quot; =&gt; new stdClass(),<br\/>    &quot;resource&quot; =&gt; tmpfile(),<br\/>    &quot;null&quot; =&gt; null,<br\/>);<br\/><br\/>\/\/ var_export -- nice, one-liner<br\/>$debug_export = var_export($demo, true);<br\/><br\/>\/\/ var_dump<br\/>ob_start();<br\/>var_dump($demo);<br\/>$debug_dump = ob_get_clean();<br\/><br\/>\/\/ print_r -- included for completeness, though not recommended<br\/>$debug_printr = print_r($demo, true);<\/code><\/pre> <\/div>\n<p><strong>The difference in output:<\/strong><\/p>\n<p><strong>var_export ($debug_export in above example)<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">array (<br\/>  &#039;bool&#039; =&gt; false,<br\/>  &#039;int&#039; =&gt; 1,<br\/>  &#039;float&#039; =&gt; 3.1400000000000001,<br\/>  &#039;string&#039; =&gt; &#039;hello world&#039;,<br\/>  &#039;array&#039; =&gt; <br\/>  array (<br\/>  ),<br\/>  &#039;object&#039; =&gt; <br\/>  stdClass::__set_state(array(<br\/>  )),<br\/>  &#039;resource&#039; =&gt; NULL, \/\/ Note that this resource pointer is now NULL<br\/>  &#039;null&#039; =&gt; NULL,<br\/>):<\/code><\/pre> <\/div>\n<p><strong>var_dump ($debug_dump in above example)<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">array(8) {<br\/>  [&quot;bool&quot;]=&gt;<br\/>  bool(false)<br\/>  [&quot;int&quot;]=&gt;<br\/>  int(1)<br\/>  [&quot;float&quot;]=&gt;<br\/>  float(3.14)<br\/>  [&quot;string&quot;]=&gt;<br\/>  string(11) &quot;hello world&quot;<br\/>  [&quot;array&quot;]=&gt;<br\/>  array(0) {<br\/>  }<br\/>  [&quot;object&quot;]=&gt;<br\/>  object(stdClass)#1 (0) {<br\/>  }<br\/>  [&quot;resource&quot;]=&gt;<br\/>  resource(4) of type (stream)<br\/>  [&quot;null&quot;]=&gt;<br\/>  NULL<br\/>}<\/code><\/pre> <\/div>\n<p><strong>print_r ($debug_printr in above example):<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">Array<br\/>(<br\/>    [bool] =&gt; <br\/>    [int] =&gt; 1<br\/>    [float] =&gt; 3.14<br\/>    [string] =&gt; hello world<br\/>    [array] =&gt; Array<br\/>        (<br\/>        )<br\/><br\/>    [object] =&gt; stdClass Object<br\/>        (<br\/>        )<br\/><br\/>    [resource] =&gt; Resource id #4<br\/>    [null] =&gt; <br\/>)<\/code><\/pre> <\/div>\n<p><strong>Caveat: var_export does not handle circular references<\/strong><\/p>\n<p><strong>If you&#8217;re trying to dump a variable with circular references, calling var_export will result in a PHP warning:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$circular = array();<br\/> $circular[&#039;self&#039;] =&amp; $circular;<br\/> var_export($circular);<\/code><\/pre> <\/div>\n<p><strong>Results in:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">Warning: var_export does not handle circular references in example.php on line 3<br\/> array (<br\/>   &#039;self&#039; =&gt; <br\/>   array (<br\/>     &#039;self&#039; =&gt; NULL,<br\/>   ),<br\/> )<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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 : [ad type=&#8221;banner&#8221;] This function displays structured information about one or more expressions that includes its type [&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}]}}