<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Prints human-readable information about a variable</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.isset.html">isset</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.serialize.html">serialize</a></div> <div class="up"><a href="ref.var.html">Variable handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.print-r" class="refentry"> <div class="refnamediv"> <h1 class="refname">print_r</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">print_r</span> — <span class="dc-title"> Prints human-readable information about a variable </span></p> </div> <div class="refsect1 description" id="refsect1-function.print-r-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>print_r</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$expression</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$return</code><span class="initializer"> = <strong><code>FALSE</code></strong></span></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>print_r()</strong></span> displays information about a variable in a way that's readable by humans. </p> <p class="para"> <span class="function"><strong>print_r()</strong></span>, <span class="function"><a href="function.var-dump.html" class="function">var_dump()</a></span> and <span class="function"><a href="function.var-export.html" class="function">var_export()</a></span> will also show protected and private properties of objects. Static class members will not be shown. </p> </div> <div class="refsect1 parameters" id="refsect1-function.print-r-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">expression</code></dt> <dd> <p class="para"> The expression to be printed. </p> </dd> <dt> <code class="parameter">return</code></dt> <dd> <p class="para"> If you would like to capture the output of <span class="function"><strong>print_r()</strong></span>, use the <code class="parameter">return</code> parameter. When this parameter is set to <strong><code>TRUE</code></strong>, <span class="function"><strong>print_r()</strong></span> will return the information rather than print it. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.print-r-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> If given a <span class="type"><a href="language.types.string.html" class="type string">string</a></span>, <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> or <span class="type"><a href="language.types.float.html" class="type float">float</a></span>, the value itself will be printed. If given an <span class="type"><a href="language.types.array.html" class="type array">array</a></span>, values will be presented in a format that shows keys and elements. Similar notation is used for <span class="type"><a href="language.types.object.html" class="type object">object</a></span>s. </p> <p class="para"> When the <code class="parameter">return</code> parameter is <strong><code>TRUE</code></strong>, this function will return a <span class="type"><a href="language.types.string.html" class="type string">string</a></span>. Otherwise, the return value is <strong><code>TRUE</code></strong>. </p> </div> <div class="refsect1 notes" id="refsect1-function.print-r-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">When the <code class="parameter">return</code> parameter is used, this function uses internal output buffering so it cannot be used inside an <span class="function"><a href="function.ob-start.html" class="function">ob_start()</a></span> callback function.</p></p></blockquote> </div> <div class="refsect1 examples" id="refsect1-function.print-r-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-6512"> <p><strong>Example #1 <span class="function"><strong>print_r()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <pre><br /><span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= array (</span><span style="color: #DD0000">'a' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">, </span><span style="color: #DD0000">'b' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'banana'</span><span style="color: #007700">, </span><span style="color: #DD0000">'c' </span><span style="color: #007700">=> array (</span><span style="color: #DD0000">'x'</span><span style="color: #007700">, </span><span style="color: #DD0000">'y'</span><span style="color: #007700">, </span><span style="color: #DD0000">'z'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r </span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?><br /></span></pre></span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> <pre> Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) ) </pre> </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-6513"> <p><strong>Example #2 <code class="parameter">return</code> parameter example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$b </span><span style="color: #007700">= array (</span><span style="color: #DD0000">'m' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'monkey'</span><span style="color: #007700">, </span><span style="color: #DD0000">'foo' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'x' </span><span style="color: #007700">=> array (</span><span style="color: #DD0000">'x'</span><span style="color: #007700">, </span><span style="color: #DD0000">'y'</span><span style="color: #007700">, </span><span style="color: #DD0000">'z'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$results </span><span style="color: #007700">= </span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">); </span><span style="color: #FF8000">// $results now contains output from print_r<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.print-r-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.ob-start.html" class="function" rel="rdfs-seeAlso">ob_start()</a> - Turn on output buffering</span></li> <li class="member"><span class="function"><a href="function.var-dump.html" class="function" rel="rdfs-seeAlso">var_dump()</a> - Dumps information about a variable</span></li> <li class="member"><span class="function"><a href="function.var-export.html" class="function" rel="rdfs-seeAlso">var_export()</a> - Outputs or returns a parsable string representation of a variable</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.isset.html">isset</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.serialize.html">serialize</a></div> <div class="up"><a href="ref.var.html">Variable handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>