{"id":12138,"date":"2010-02-12T00:36:27","date_gmt":"2010-02-11T22:36:27","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=12138"},"modified":"2010-02-12T00:36:27","modified_gmt":"2010-02-11T22:36:27","slug":"php-variables-strings-and-curly-braces","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2010\/02\/12\/php-variables-strings-and-curly-braces\/","title":{"rendered":"PHP variables, strings, and curly braces"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>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 example:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n# my way\r\necho &quot;Result: ${blah&#x5B;'something']}\\n&quot;;\r\n# the highway\r\necho &quot;Result: {$blah&#x5B;'something']}\\n&quot;;\r\n<\/pre>\n<p>While considering a number of examples, there seems to be no difference &#8211; both ways work. \u00a0We&#8217;d still need to pick one for consistency reasons though. \u00a0And I, as an ex-Perl programmer, was suggesting that we should use the dollar sign on the outside of the expression. \u00a0This how I remember it being in Perl (and PHP originated from Perl)\u00a0. \u00a0This is how I am used to it. \u00a0And this is how makes most sense to me &#8211; a dollar sign immediately warns the programmer that the variable is ahead.<\/p>\n<p>However, after consulting <a href=\"http:\/\/php.net\/manual\/en\/language.types.string.php\">PHP documentation<\/a>, I was proved wrong. \u00a0It is said that both ways often work, but it is much safer to use the dollar sign on the inside. \u00a0The manual page even provides a few examples where the dollar on the outside won&#8217;t work (such in case with objects).<\/p>\n<p>While this is just a small thing to know and get used to, it still looks annoying to me.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>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 example: # my way echo &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2010\/02\/12\/php-variables-strings-and-curly-braces\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PHP variables, strings, and curly braces<\/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":[36,38],"keyring_services":[],"class_list":["post-12138","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","tag-perl","tag-php"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":11461,"url":"https:\/\/mamchenkov.net\/wordpress\/2008\/12\/12\/perl-vs-php-variable-scoping\/","url_meta":{"origin":12138,"position":0},"title":"Perl vs. PHP : variable scoping","author":"Leonid Mamchenkov","date":"December 12, 2008","format":false,"excerpt":"I'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\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":7961,"url":"https:\/\/mamchenkov.net\/wordpress\/2004\/09\/26\/php-in-contrast-to-perl\/","url_meta":{"origin":12138,"position":1},"title":"PHP IN CONTRAST TO PERL","author":"Leonid Mamchenkov","date":"September 26, 2004","format":false,"excerpt":"There are plenty of holy wars on the web that argue about superiority of PHP over Perl and vice versa. There are many people who prefer PHP, and there are many others who prefer Perl. I am, of course, in the Perl camp. I have programmed both Perl and PHP\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":204,"url":"https:\/\/mamchenkov.net\/wordpress\/2003\/04\/28\/porting-php-to-perl\/","url_meta":{"origin":12138,"position":2},"title":"Porting PHP to Perl","author":"Leonid Mamchenkov","date":"April 28, 2003","format":false,"excerpt":"I am doing lots of perl programming these days. And there is yet lots more to come. I am currently porting most of my PHP code base to Perl. This provides a good practice, as well as code review and clean-up. Although, I know that porting is somewhat easier then\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":12138,"position":3},"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":22151,"url":"https:\/\/mamchenkov.net\/wordpress\/2014\/07\/08\/testframeworkinatweet-php-a-php-unit-testing-framework-in-a-tweet\/","url_meta":{"origin":12138,"position":4},"title":"TestFrameworkInATweet.php &#8211; a PHP unit testing framework in a tweet","author":"Leonid Mamchenkov","date":"July 8, 2014","format":"link","excerpt":"TestFrameworkInATweet.php - a PHP unit testing framework in a tweet. \u00a0This is brilliant in its own way. \u00a0It reminds me of the Perl Golf Apocalypse.","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":11200,"url":"https:\/\/mamchenkov.net\/wordpress\/2008\/05\/19\/php-6-hopefully-not-the-end-of-the-road\/","url_meta":{"origin":12138,"position":5},"title":"PHP 6 &#8211; hopefully not the end of the road","author":"Leonid Mamchenkov","date":"May 19, 2008","format":false,"excerpt":"I've heard plenty of positive buzz about PHP 6 in the last few weeks.\u00a0 Yes, it's coming out.\u00a0 Yes, it brings quite a few improvements, including better Unicode support, better security, and more help for larger projects through namespaces.\u00a0 However, I hope that it won't be the last PHP release,\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\/12138","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=12138"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/12138\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=12138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=12138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=12138"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=12138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}