{"id":86,"date":"2005-04-23T13:21:05","date_gmt":"2005-04-23T19:21:05","guid":{"rendered":"\/?p=86"},"modified":"2005-04-23T13:21:05","modified_gmt":"2005-04-23T19:21:05","slug":"php-gotcha","status":"publish","type":"post","link":"https:\/\/www.codedread.com\/blog\/archives\/2005\/04\/23\/php-gotcha\/","title":{"rendered":"PHP Gotcha"},"content":{"rendered":"<p>After I wrote <a href=\"http:\/\/blog.codedread.com\/archives\/2005\/04\/21\/ajax-gotchas\/\">this entry<\/a>, I knew I had forgotten one \"gotcha\" from my little Remote Scripting experience.  I remembered it today.  It involves PHP and the annoying aspect of having to declare within a function when you want to use a global variable:<\/p>\n<div class=\"code\">\n$gSomeVariable = 5;<br \/>\nfunction somefunc() {<br \/>\n&#160;&#160;&#160;printf(\"%d\", gSomeVariable);<br \/>\n}\n<\/div>\n<p>The above will not produce \"5\".  Without explicitly telling PHP that gSomeVariable is a global variable, it will assume it is a local variable and currently undefined.<\/p>\n<p>Do you know how many time this has tripped me up?  The correct PHP code is:<\/p>\n<div class=\"code\">\n$gSomeVariable = 5;<br \/>\nfunction somefunc() {<br \/>\n<strong>&#160;&#160;&#160;global $gSomeVariable;<\/strong><br \/>\n&#160;&#160;&#160;printf(\"%d\", gSomeVariable);<br \/>\n}\n<\/div>\n<p>For all the times I made this mistake:   AAAAAARRRRRGGGGHHHH!!!!!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After I wrote this entry, I knew I had forgotten one &#8220;gotcha&#8221; from my little Remote Scripting experience. I remembered it today. It involves PHP and the annoying aspect of having to declare within a function when you want to use a global variable: $gSomeVariable = 5; function somefunc() { &#160;&#160;&#160;printf(&#8220;%d&#8221;, gSomeVariable); } The above [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,25,11,14,28],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-php","category-software","category-technology","category-tips","category-web"],"_links":{"self":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":0,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}