<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Catch My Fame &#187; PHP</title>
	<atom:link href="http://www.catchmyfame.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.catchmyfame.com</link>
	<description>A web designoper&#039;s journal</description>
	<lastBuildDate>Thu, 02 Feb 2012 14:18:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>You Learn Something New Everyday</title>
		<link>http://www.catchmyfame.com/2008/12/18/you-learn-something-new-everyday/</link>
		<comments>http://www.catchmyfame.com/2008/12/18/you-learn-something-new-everyday/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 14:57:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.catchmyfame.com/?p=48</guid>
		<description><![CDATA[I&#8217;ve been coding in PHP for probably eight years and thought I knew the language pretty well. Was I surprised the other day when I came across someone else&#8217;s code and stared at it blankly, wondering why they had done what they did, and why wasn&#8217;t it generating an error. Specifically they had something like [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been coding in PHP for probably eight years and thought I knew the language pretty well. Was I surprised the other day when I came across someone else&#8217;s code and stared at it blankly, wondering why they had done what they did, and why wasn&#8217;t it generating an error. Specifically they had something like this:</p>
<pre>echo "Your IP address is {$_SERVER['<span class="term">REMOTE_ADDR'</span>]}";</pre>
<p>Why were those curly brackets in the echo statement and why wasn&#8217;t this giving me errors? I removed the curly brackets and as expected, I received syntax errors. Apparently the {} were magical. Now in my own code, I would typically write the above line like:</p>
<pre>echo "Your IP address is " . $_SERVER['<span class="term">REMOTE_ADDR'</span>];</pre>
<p>or</p>
<pre>echo "Your IP address is $_SERVER[<span class="term">REMOTE_ADDR</span>]";</pre>
<p>Now method one is perfectly valid code, but it&#8217;s a bit ugly having to starty and stop the string to insert a variable. Method 2 is somewhat wrong as the quotes have been removed from the server array key.  According to php.net, &#8220;Always use quotes around a string literal array index. For example,     <em>$foo['bar']</em> is correct, while     <em>$foo[bar]</em> is not.&#8221; But with this new, more compact method, I could make my code cleaner.  I was off to learn more.</p>
<p>After some <a href="http://www.php.net/manual/en/language.types.string.php">searching</a> I discovered that PHP will allow you to place curly brackets around a variable in a string (either around the entire variable or with the dollar sign sticking out). &#8220;If a dollar sign (<em>$</em>) is encountered, the parser will      greedily take as many tokens as possible to form a valid variable name.      Enclose the variable name in curly braces to explicitly specify the end of      the name. Similarly, an <a class="type array" href="http://www.php.net/manual/en/language.types.array.php">array</a> index or an <a class="type object" href="http://www.php.net/manual/en/language.types.object.php">object</a> property      can be parsed. With array indices, the closing square bracket      (<em>]</em>) marks the end of the index. The same rules apply to      object properties as to simple variables.     &#8221; Who knew? I must&#8217;ve missed this day during PHP 101 class. Here&#8217;s the example from the manual:</p>
<pre><code><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php
$beer </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'Heineken'</span><span style="color: #007700;">;
echo </span><span style="color: #dd0000;">"</span><span style="color: #0000bb;">$beer</span><span style="color: #dd0000;">'s taste is great"</span><span style="color: #007700;">; </span><span style="color: #ff8000;">// works; "'" is an invalid character for variable names</span><span style="color: #007700;">
echo </span><span style="color: #dd0000;">"He drank some </span><span style="color: #0000bb;">$beers</span><span style="color: #dd0000;">"</span><span style="color: #007700;">;   </span><span style="color: #ff8000;">// won't work; 's' is a valid character for variable names but the variable is "$beer"</span><span style="color: #007700;">
echo </span><span style="color: #dd0000;">"He drank some </span><span style="color: #007700;">${</span><span style="color: #0000bb;">beer</span><span style="color: #007700;">}</span><span style="color: #dd0000;">s"</span><span style="color: #007700;">; </span><span style="color: #ff8000;">// works</span><span style="color: #007700;">
echo </span><span style="color: #dd0000;">"He drank some </span><span style="color: #007700;">{</span><span style="color: #0000bb;">$beer</span><span style="color: #007700;">}</span><span style="color: #dd0000;">s"</span><span style="color: #007700;">; </span><span style="color: #ff8000;">// works</span><span style="color: #0000bb;">
?&gt;

</span></span></code></pre>
<p>So in a nushell, when using variables in a string you are allowed to enclose the varable in curly brackets to allow variable expansion without having to jump in and out of quotes, or drop quotes. Who doesn&#8217;t love PHP?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.catchmyfame.com/2008/12/18/you-learn-something-new-everyday/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

