{"id":11461,"date":"2008-12-12T13:35:40","date_gmt":"2008-12-12T10:35:40","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=11461"},"modified":"2008-12-12T13:35:40","modified_gmt":"2008-12-12T10:35:40","slug":"perl-vs-php-variable-scoping","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2008\/12\/12\/perl-vs-php-variable-scoping\/","title":{"rendered":"Perl vs. PHP : variable scoping"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>I&#8217;ve mentioned quite a few times that I am a big fan of Perl programming languge.\u00a0 However, most of my programming time these days is spent in PHP.\u00a0 The languages are often similar, with PHP having its roots in Perl, and Perl being such a influence in the world of programming languages.\u00a0 This similarity is often very helpful.\u00a0 However there are a few difference, some of which are obvious and others are not.<\/p>\n<p>One such difference that I came up recently (in someone else&#8217;s code though), was about variable scoping.\u00a0 Consider an example in Perl:<\/p>\n<pre>#!\/usr\/bin\/perl -w\r\nuse strict;\r\nmy @values = qw(foo bar hello world);\r\nforeach my $value (@values) {\r\n\u00a0\u00a0\u00a0 print \"Inside loop value = $value\\n\";\r\n}\r\nprint \"Outside loop value = $value\\n\";<\/pre>\n<p>The above script will generate a compilation error due to undefined variable $value.\u00a0 The one outside the loop.<\/p>\n<p>A very similar code in PHP though:<\/p>\n<pre>#!\/usr\/bin\/php\r\n&lt;?php\r\n$values = array('foo','bar','hello','world');\r\nforeach ($values as $value) {\r\n\u00a0\u00a0\u00a0 print \"Inside loop value = $value\\n\";\r\n}\r\nprint \"Outside loop value = $value\\n\";\r\n?&gt;<\/pre>\n<p>Will output the following:<\/p>\n<pre>Inside loop value = foo\r\nInside loop value = bar\r\nInside loop value = hello\r\nInside loop value = world\r\nOutside loop value = world<\/pre>\n<p>In Perl, variable $value is scoped inside the loop.\u00a0 Once the execution is out of the loop, there is no such thing as $value anymore, hence the compilation error (due to the use of strict and warnings).\u00a0 In PHP, $value is in global scope, so the last value &#8220;world&#8221; is carried further down the road.\u00a0 In case you reuse variable names in different places of your program, counting on scope to be different, you might get some really interesting and totally unexpected results.\u00a0 And they won&#8217;t be too easy to track down too.\u00a0 Be warned.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>I&#8217;ve mentioned quite a few times that I am a big fan of Perl programming languge.\u00a0 However, most of my programming time these days is spent in PHP.\u00a0 The languages are often similar, with PHP having its roots in Perl, and Perl being such a influence in the world of programming languages.\u00a0 This similarity is &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2008\/12\/12\/perl-vs-php-variable-scoping\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Perl vs. PHP : variable scoping<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18],"tags":[859,36,38],"keyring_services":[],"class_list":["post-11461","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","tag-experience","tag-perl","tag-php"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":12138,"url":"https:\/\/mamchenkov.net\/wordpress\/2010\/02\/12\/php-variables-strings-and-curly-braces\/","url_meta":{"origin":11461,"position":0},"title":"PHP variables, strings, and curly braces","author":"Leonid Mamchenkov","date":"February 12, 2010","format":false,"excerpt":"For the last couple of days we had a number arguments at work about what is the best way to surround a complex PHP variable inside a double-quoted string. \u00a0More specifically, should the sigil ($, dollar sign) be on the inside of the braces or on the outside. \u00a0Consider an\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7402,"url":"https:\/\/mamchenkov.net\/wordpress\/2004\/03\/30\/php-zopepython-and-perl\/","url_meta":{"origin":11461,"position":1},"title":"PHP, Zope\/Python, and Perl","author":"Leonid Mamchenkov","date":"March 30, 2004","format":false,"excerpt":"There is an article at kuro5hin.net called \"Switching from PHP to Zope\/Python\". This should provide for an interesting read for all those poor PHP souls. As for me - I use Perl and I am not planning to change any time soon. There are two major bonuses in Perl for\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9613,"url":"https:\/\/mamchenkov.net\/wordpress\/2005\/10\/26\/missing-some-regexp-knowledge\/","url_meta":{"origin":11461,"position":2},"title":"Missing some regexp knowledge","author":"Leonid Mamchenkov","date":"October 26, 2005","format":false,"excerpt":"Once in a while I come across some topic that I should have learned a billion years ago. Something very basic and useful that I had a chance of seeing many times but haven't, or probably had and ignored it. Today I found out that I was missing an important\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":12235,"url":"https:\/\/mamchenkov.net\/wordpress\/2010\/03\/08\/whatever-happened-to-programming\/","url_meta":{"origin":11461,"position":3},"title":"Whatever happened to programming","author":"Leonid Mamchenkov","date":"March 8, 2010","format":false,"excerpt":"Via this Slashdot post I came across an excellent blog rant - Whatever happened to programming (and the follow-up). \u00a0Subject in focus - modern programming, and how boring it have become (mostly). Today, I mostly paste libraries together. \u00a0So do you, most likely, if you work in software. \u00a0Doesn\u2019t that\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26254,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/07\/18\/the-slashdot-interview-with-larry-wall\/","url_meta":{"origin":11461,"position":4},"title":"The Slashdot Interview With Larry Wall","author":"Leonid Mamchenkov","date":"July 18, 2016","format":false,"excerpt":"Slashdot runs the interview with Larry Wall, the creator of Perl programming language. \u00a0There is a wide variety of questions. \u00a0Some are technical - about Perl 6, comparison to other programming languages (Python, PHP), Perl in the browser, etc. \u00a0Some are more generic - what kind of tools Larry uses,\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11477,"url":"https:\/\/mamchenkov.net\/wordpress\/2009\/01\/04\/programming-religions\/","url_meta":{"origin":11461,"position":5},"title":"Programming religions","author":"Leonid Mamchenkov","date":"January 4, 2009","format":false,"excerpt":"I'm slowly catching up with the news stream and all the jokes of the last few weeks.\u00a0 \"If programming languages were religions\" is a nice one.\u00a0 Here is PHP, which I spent the most time with now: PHP would be Cafeteria Christianity - Fights with Java for the web market.\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/11461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=11461"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/11461\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=11461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=11461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=11461"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=11461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}