{"id":1453,"date":"2018-02-05T11:37:45","date_gmt":"2018-02-05T16:37:45","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1453"},"modified":"2018-02-22T11:44:05","modified_gmt":"2018-02-22T16:44:05","slug":"php-print-information-variables-arrays-objects","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/02\/05\/php-print-information-variables-arrays-objects\/","title":{"rendered":"PHP: How to print information about variables, arrays, and objects"},"content":{"rendered":"<p>To print information about variables, arrays or objects in PHP, we can use the following functions:<\/p>\n<ul>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">print_r()<\/code><\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">var_dump() <\/code><\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">var_export()<\/code><\/li>\n<\/ul>\n<p><strong>print_r( )<\/strong><\/p>\n<p>According to this <a href=\"https:\/\/stackoverflow.com\/questions\/1647322\/whats-the-difference-between-echo-print-and-print-r-in-php\">explanation<\/a> on stackoverflow.com, <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">print_r()<\/code>:<\/p>\n<ul>\n<li>Outputs a human-readable representation of any one value<\/li>\n<li>Accepts not just strings but other types including arrays and objects, formatting them to be readable<\/li>\n<li>Useful when debugging<\/li>\n<li>May return its output as a return value (instead of echoing) if the second optional argument is given<\/li>\n<\/ul>\n<p>Ostensibly, the &#8216;r&#8217; in print_r stands for &#8216;readable&#8217;. For example, setting up an associative array and using print_r to print it in a human readable format:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\n$a = array ('a' =&gt; 'apple', 'b' =&gt; 'banana', 'c' =&gt; array ('x', 'y', 'z'));\r\nprint_r ($a);\r\n?&gt;<\/pre>\n<p>yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">Array ( [a] =&gt; apple [b] =&gt; banana [c] =&gt; Array ( [0] =&gt; x [1] =&gt; y [2] =&gt; z ) )<\/pre>\n<p>For more information about <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">print_r()<\/code>, see:<br \/>\n<a href=\"http:\/\/php.net\/manual\/en\/function.print-r.php\">http:\/\/php.net\/manual\/en\/function.print-r.php<\/a><\/p>\n<p>&nbsp;<\/p>\n<hr id=\"dashed_hr\" style=\"background-color: #ffffff;\" \/>\n<p><strong>var_dump( )<\/strong><\/p>\n<p>Here is what php.net says about <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">var_dump()<\/code>:<\/p>\n<blockquote><p>This function 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.<\/p><\/blockquote>\n<p>According to this <a href=\"https:\/\/stackoverflow.com\/questions\/1647322\/whats-the-difference-between-echo-print-and-print-r-in-php\">explanation<\/a> on stackoverflow.com, <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">var_dump()<\/code>:<\/p>\n<blockquote>\n<ul>\n<li>Outputs a human-readable representation of one or more values separated by commas<\/li>\n<li>Accepts not just strings but other types including arrays and objects, formatting them to be readable<\/li>\n<li>Uses a different output format to print_r(), for example it also prints the type of values<\/li>\n<li>Useful when debugging<\/li>\n<li>No return value<\/li>\n<\/ul>\n<\/blockquote>\n<p>For example,\u00a0 let&#8217;s set up an array that contains two integers and a sub-array. <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">var_dump()<\/code> will not only show the contents of the array, but will also show the data types and lengths of each value in the array:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\n$a = array(1, 2, array(\"apple\", \"banana\", \"cherry\"));\r\nvar_dump($a);\r\n?&gt;\r\n<\/pre>\n<p>yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">array(3) { [0]=&gt; int(1) [1]=&gt; int(2) [2]=&gt; array(3) { [0]=&gt; string(5) \"apple\" [1]=&gt; string(6) \"banana\" [2]=&gt; string(6) \"cherry\" } }<\/pre>\n<p>Another example of using <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">var_dump()<\/code> to get data type of a variable:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\n$b = 3.1;\r\n$c = true;\r\nvar_dump($b, $c);\r\n?&gt;<\/pre>\n<p>which yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">float(3.1) bool(true)<\/pre>\n<p>For more information about <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">var_dump()<\/code>, see:<br \/>\n<a href=\"http:\/\/php.net\/manual\/en\/function.var-dump.php\">http:\/\/php.net\/manual\/en\/function.var-dump.php<\/a><\/p>\n<p>&nbsp;<\/p>\n<hr id=\"dashed_hr\" style=\"background-color: #ffffff;\" \/>\n<p><strong>var_export( )<\/strong><\/p>\n<p>Here is what php.net says about <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">var_export()<\/code>:<\/p>\n<blockquote>\n<p class=\"refpurpose\"><span class=\"refname\">var_export<\/span> \u2014 <span class=\"dc-title\">Outputs or returns a parsable string representation of a variable<\/span><\/p>\n<p><span class=\"function\">var_export() gets structured information about the given variable. It is similar to var_dump() with one exception: the returned representation is valid PHP code. <strong><br \/>\n<\/strong><\/span><\/p><\/blockquote>\n<p>According to this <a href=\"https:\/\/stackoverflow.com\/questions\/1647322\/whats-the-difference-between-echo-print-and-print-r-in-php\">explanation<\/a> on stackoverflow.com, <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">var_export()<\/code>:<\/p>\n<blockquote>\n<ul>\n<li>Outputs a human-readable <em>and PHP-executable<\/em> representation of any <em>one<\/em> value<\/li>\n<li>Accepts not just strings but other types including arrays and objects, formatting them to be readable<\/li>\n<li>Uses a different output format to both <code>print_r()<\/code> and <code>var_dump()<\/code> &#8211; resulting output is valid PHP code!<\/li>\n<li>Useful when debugging<\/li>\n<li>May return its output as a return value (instead of echoing) if the second optional argument is given<\/li>\n<\/ul>\n<\/blockquote>\n<p>For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\n$a = array (1, 2, array (\"a\", \"b\", \"c\"));\r\nvar_export($a);\r\n?&gt;\r\n<\/pre>\n<p>which produces:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">array ( 0 =&gt; 1, 1 =&gt; 2, 2 =&gt; array ( 0 =&gt; 'a', 1 =&gt; 'b', 2 =&gt; 'c', ), )<\/pre>\n<p>Notice the difference between this output and the output for print_r.<\/p>\n<p>For more information about <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">var_export()<\/code>, see:<br \/>\n<a href=\"http:\/\/php.net\/manual\/en\/function.var-export.php\">http:\/\/php.net\/manual\/en\/function.var-export.php<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To print information about variables, arrays or objects in PHP, we can use the following functions: print_r() var_dump() var_export() print_r( ) According to this explanation on stackoverflow.com, print_r(): Outputs a human-readable representation of any one value Accepts not just strings but other types including arrays and objects, formatting them to be readable Useful when debugging &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/02\/05\/php-print-information-variables-arrays-objects\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PHP: How to print information about variables, arrays, and objects<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97],"tags":[69,99,100,101],"class_list":["post-1453","post","type-post","status-publish","format-standard","hentry","category-php-language","tag-php","tag-print_r","tag-var_dump","tag-var_export"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1453"}],"version-history":[{"count":17,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1453\/revisions"}],"predecessor-version":[{"id":1522,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1453\/revisions\/1522"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}