{"id":203,"date":"2007-02-18T18:56:12","date_gmt":"2007-02-19T00:56:12","guid":{"rendered":"http:\/\/blog.the-erm.com\/archives\/203"},"modified":"2007-02-18T19:25:26","modified_gmt":"2007-02-19T01:25:26","slug":"the-most-useful-php-functions-ever","status":"publish","type":"post","link":"https:\/\/blog.the-erm.com\/?p=203","title":{"rendered":"The most useful php functions EVER!"},"content":{"rendered":"<p>Many times when you&#8217;re writing a php script, you need to put things in arrays.<\/p>\n<p>You might be thinking to yourself why doesn&#8217;t this section of code work?<br \/>\n<!--more--><\/p>\n<p>Well let me introduce you to my little friend <a href=\"http:\/\/us2.php.net\/manual\/en\/function.print-r.php\">print_r()<\/a> and is big brother <a href=\"http:\/\/us2.php.net\/manual\/en\/function.var-dump.php\">var_dump()<\/a>.<\/p>\n<p>These 2 functions can save you hours of frustration, and also keeps you from looking like a total <a href=\"http:\/\/en.wikipedia.org\/wiki\/Lamer\">lamer<\/a> in ##php.<\/p>\n<p>Here&#8217;s some code taken from php.net if you didn&#8217;t check out the link to print_r.<\/p>\n<pre>&lt;pre&gt;\r\n&lt;?php<br \/>\r\n  $a = array ('a' => 'apple', 'b' => 'banana',\r\n              'c' => array ('x', 'y', 'z'));\r\n  print_r ($a);\r\n?&gt;\r\n&lt;\/pre&gt;\r\n<\/pre>\n<p>Outputs:<\/p>\n<pre>\r\n&lt;pre&gt;\r\nArray\r\n(\r\n    [a] => apple\r\n    [b] => banana\r\n    [c] => Array\r\n        (\r\n            [0] => x\r\n            [1] => y\r\n            [2] => z\r\n        )\r\n)\r\n&lt;\/pre&gt;\r\n<\/pre>\n<p>The great thing about print_r() and var_dump() is they work with objects as well.<\/p>\n<p>Since you are more than likely a newbie at coding in php, let me explain a little more in depth as to why this is so important.<\/p>\n<p>Let&#8217;s say you&#8217;re building a site with a form.<\/p>\n<pre>\r\n&lt;form method=\"post\"&gt;\r\n  &lt;input type=\"text\" name=\"foo\"&gt;\r\n  &lt;input type=\"submit\" name=\"action\" value=\"Send\"&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<p>After you submit it where did the data go, what is it doing?  How is it formatted?<\/p>\n<p>Well that&#8217;s where print_r() and var_dump() come into play.<\/p>\n<p>When you are writing a page add this code to the bottom of the page:<\/p>\n<pre>\r\n&lt;b&gt;$_GET&lt;\/b&gt;\r\n&lt;pre&gt;\r\n  &lt;?php print_r($_GET); ?&gt;\r\n&lt;\/pre&gt;\r\n&lt;b&gt;$_POST&lt;\/b&gt;\r\n&lt;pre&gt;\r\n  &lt;?php print_r($_POST); ?&gt;\r\n&lt;\/pre&gt;\r\n&lt;b&gt;$_SESSION&lt;\/b&gt;\r\n&lt;pre&gt;\r\n  &lt;?php print_r($_SESSION); ?&gt;\r\n&lt;\/pre&gt;\r\n<\/pre>\n<p>After you do that you can get a pretty good idea of what&#8217;s going on inside your page while you&#8217;re codeing it.  You can also do this with objects as well.<\/p>\n<p>I mentioned to someone in ##php that if I ever wrote a manual print_r() and var_dump() would be the on the first page.  They said not to forget about <a href=\"http:\/\/us2.php.net\/manual\/en\/function.debug-backtrace.php\">debug_backtrace()<\/a>  This is a nifty function indeed.<\/p>\n<p>I hope this helps you look less like a lamer in ##php and more like a knowledgable programmer.  I use print_r so much I even wrote these 2 functions to help me out.<\/p>\n<pre>&lt;?php\r\nfunction ent($string) {\r\n    if (is_array($string) || is_object($string)) {\r\n        return '&lt;pre&gt;'. htmlentities(print_r($string,true),\r\n                              ENT_QUOTES).'&lt;\/pre&gt;';\r\n    }\r\n    return htmlentities($string, ENT_QUOTES);\r\n}\r\n\r\nfunction eent($string) {\r\n    echo ent($string);\r\n}\r\n\r\n?&gt;\r\n<\/pre>\n<p>Happy Coding,<br \/>\nErm<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many times when you&#8217;re writing a php script, you need to put things in arrays. You might be thinking to yourself why doesn&#8217;t this section of code work?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,27],"tags":[],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-php","category-tutorials"],"_links":{"self":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=203"}],"version-history":[{"count":0,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}