<?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>David Otton &#187; PHP Tricks</title>
	<atom:link href="http://www.otton.org/category/programming/php-tips-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.otton.org</link>
	<description>Look! Bunnies!</description>
	<lastBuildDate>Wed, 06 Jan 2010 14:32:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>More switch() Abuse &#8211; Switching on Multiple Values</title>
		<link>http://www.otton.org/2009/11/22/more-php-switch-abuse-switching-on-multiple-values/</link>
		<comments>http://www.otton.org/2009/11/22/more-php-switch-abuse-switching-on-multiple-values/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 22:38:11 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[PHP Tricks]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[branching]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[switch]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=400</guid>
		<description><![CDATA[Due to working with Drupal for most of this year, I&#8217;ve seen this construct so many times it&#8217;s killed me a little inside:

switch&#40;$a&#41; &#123;
    case TRUE:
        switch&#40;$b&#41; &#123;
            case TRUE:
     [...]]]></description>
			<content:encoded><![CDATA[<p>Due to working with Drupal for most of this year, I&#8217;ve seen this construct so many times it&#8217;s killed me a little inside:</p>
<p><code>
<pre><span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">TRUE</span>:
        <span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$b</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">TRUE</span>:
                <span style="color: #b1b100;">break</span>;
            <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">FALSE</span>:
                <span style="color: #b1b100;">break</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">FALSE</span>:
        <span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$b</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">TRUE</span>:
                <span style="color: #b1b100;">break</span>;
            <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">FALSE</span>:
                <span style="color: #b1b100;">break</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">break</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p></code></p>
<p>Someone I work with, who wishes to keep his name off the net, came up with this:</p>
<p><code>
<pre><span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span>, <span style="color: #0000ff;">$b</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">break</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p></code></p>
<p>I&#8217;d rather avoid the need for nested switches altogether, but this construct makes it easy to collapse similar branches, and is, I think, a useful addition to my toolkit:</p>
<p><code>
<pre><span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span>,<span style="color: #0000ff;">$b</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>:
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>:
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>:
    <span style="color: #000000; font-weight: bold;">default</span>:
        <span style="color: #b1b100;">break</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p></code></p>
<p>It&#8217;s worth mentioning that arrays are compared for equality (==) rather than identicality (===).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2009/11/22/more-php-switch-abuse-switching-on-multiple-values/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Supid PHP Tricks: Broken Switch</title>
		<link>http://www.otton.org/2009/06/05/supid-php-tricks-broken-switch/</link>
		<comments>http://www.otton.org/2009/06/05/supid-php-tricks-broken-switch/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 12:03:46 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[PHP Tricks]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=388</guid>
		<description><![CDATA[A fragment of PHP that really shouldn&#8217;t work. I hope nobody ever finds a use for this:

switch&#40;$n&#41;
&#123;
    case 1;
        echo 'one';
        break;
    case 2;
        echo 'two';
   [...]]]></description>
			<content:encoded><![CDATA[<p>A fragment of PHP that really shouldn&#8217;t work. I hope nobody ever finds a use for this:</p>
<p><code>
<pre><span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$n</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span>;
        <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'one'</span>;
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span>;
        <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'two'</span>;
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span>;
        <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'three'</span>;
        <span style="color: #b1b100;">break</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2009/06/05/supid-php-tricks-broken-switch/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>I Learn Something New Every Day &#8211; isset()</title>
		<link>http://www.otton.org/2009/04/16/i-learn-something-new-every-day-isset/</link>
		<comments>http://www.otton.org/2009/04/16/i-learn-something-new-every-day-isset/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 12:10:30 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[PHP Tricks]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=377</guid>
		<description><![CDATA[Check this out:

if &#40;isset&#40;$a, $b, $c&#41;&#41;
&#123;
    unset&#40;$a, $b, $c&#41;;
&#125;

Mulitple arguments. Looks like they&#8217;ve been there since an early PHP4 release, at least. Why does nobody tell me these things?
]]></description>
			<content:encoded><![CDATA[<p>Check this out:<br />
<code>
<pre><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span>, <span style="color: #0000ff;">$b</span>, <span style="color: #0000ff;">$c</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <a href="http://www.php.net/unset"><span style="color: #000066;">unset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span>, <span style="color: #0000ff;">$b</span>, <span style="color: #0000ff;">$c</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p></code></p>
<p>Mulitple arguments. Looks like they&#8217;ve been there since an early PHP4 release, at least. Why does nobody tell me these things?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2009/04/16/i-learn-something-new-every-day-isset/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP Tip: Classes Aren&#8217;t Derived From stdClass</title>
		<link>http://www.otton.org/2008/11/11/php-base-class-stdclass/</link>
		<comments>http://www.otton.org/2008/11/11/php-base-class-stdclass/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 17:49:54 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[PHP Tricks]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=324</guid>
		<description><![CDATA[A base class is &#34;a class from which other classes are derived&#34;.
Many OO languages have the concept of a single base class from which all other classes are explicitly or implicitly descended. For example, Ruby, Java and .NET all have Object.
It&#8217;s a very common belief that PHP implements stdClass as a base class for all [...]]]></description>
			<content:encoded><![CDATA[<p>A base class is &quot;<a href="http://en.wikipedia.org/wiki/Superclass_(computer_science)">a class from which other classes are derived</a>&quot;.</p>
<p>Many OO languages have the concept of a single base class from which all other classes are explicitly or implicitly descended. For example, Ruby, Java and .NET all have <tt>Object</tt>.</p>
<p>It&#8217;s a very <a href="http://www.google.co.uk/search?hl=en&#038;q=php%20%22base%20class%22%20stdClass&#038;meta=">common belief</a> that PHP implements stdClass as a base class for all objects, but this is in fact not the case:</p>
<p><code>
<pre><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DoesNotExtend <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DoesExtend extends stdClass <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$doesNotExtend</span> = <span style="color: #000000; font-weight: bold;">new</span> DoesNotExtend<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$doesExtend</span> = <span style="color: #000000; font-weight: bold;">new</span> DoesExtend<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a href="http://www.php.net/var_dump"><span style="color: #000066;">var_dump</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$doesNotExtend</span> instanceof stdClass<span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/var_dump"><span style="color: #000066;">var_dump</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$doesExtend</span> instanceof stdClass<span style="color: #66cc66;">&#41;</span>;</pre>
<p></code></p>
<p>Outputs:</p>
<p>bool(false)<br />
bool(true)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2008/11/11/php-base-class-stdclass/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Neat PHP Tricks: How To Assign References to Globals</title>
		<link>http://www.otton.org/2008/11/09/neat-php-tricks-assign-references-globals-workaround/</link>
		<comments>http://www.otton.org/2008/11/09/neat-php-tricks-assign-references-globals-workaround/#comments</comments>
		<pubDate>Sat, 08 Nov 2008 23:51:12 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[PHP Tricks]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=295</guid>
		<description><![CDATA[When a language is defined by its implementation rather than a standard, it can sometimes be tricky to decide what should be considered correct behaviour and what should be considered an implementation bug.
What follows isn&#8217;t so much a PHP trick as a fix for something that really should work, but doesn&#8217;t. Although the manual implies [...]]]></description>
			<content:encoded><![CDATA[<p>When a language is defined by its implementation rather than a standard, it can sometimes be tricky to decide what should be considered correct behaviour and what should be considered an implementation bug.</p>
<p>What follows isn&#8217;t so much a PHP trick as a fix for something that really should work, but doesn&#8217;t. Although the manual implies that the behaviour described below is specific to Zend Engine 1, all my tests were performed against Zend Engine 2.2, PHP 5.2.5.</p>
<p>Quoting from the manual:</p>
<blockquote><p>The Zend Engine 1, driving PHP 4, implements the static and global modifier for variables in terms of references. For example, a true global variable imported inside a function scope with the global statement actually creates a reference to the global variable. This can lead to unexpected behaviour which the following example addresses:</p>
<p><code>
<pre><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> test_global_ref<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <a href="http://www.php.net/global"><span style="color: #000066;">global</span></a> <span style="color: #0000ff;">$obj</span>;
    <span style="color: #0000ff;">$obj</span> = <span style="color: #000000; font-weight: bold;">&amp;new</span> stdclass;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> test_global_noref<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <a href="http://www.php.net/global"><span style="color: #000066;">global</span></a> <span style="color: #0000ff;">$obj</span>;
    <span style="color: #0000ff;">$obj</span> = <span style="color: #000000; font-weight: bold;">new</span> stdclass;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
test_global_ref<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/var_dump"><span style="color: #000066;">var_dump</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$obj</span><span style="color: #66cc66;">&#41;</span>;
test_global_noref<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/var_dump"><span style="color: #000066;">var_dump</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$obj</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre>
<p></code></p>
<p>Executing this example will result in the following output:</p>
<p><code>NULL<br />
object(stdClass)(0) {}</code>
</p></blockquote>
<p> &#8211; <a href="http://uk2.php.net/static">http://uk2.php.net/static</a></p>
<p>The example above uses instances of stdClass, but attempts to assign references to scalar values, arrays or resources to global variables have the same result: the attempt to modify the global fails without error (even with error_reporting(E_ALL)), and the global retains whatever value it had before the function call.</p>
<p>The workaround is very simple &#8211; assign the reference via the $GLOBALS superglobal:</p>
<p><code>
<pre><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> test_global_ref<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$GLOBALS</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'obj'</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">&amp;new</span> stdclass;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
test_global_ref<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/var_dump"><span style="color: #000066;">var_dump</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$obj</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p></code></p>
<p>Outputs:</p>
<p><code>object(stdClass)#1 (0) { }</code></p>
<p>I think one of these scripts must expose an implementation bug &#8211; either assigning a reference to a global variable should work, in which case the first script <em>should not</em> fail, or it should not work and the second script <em>should</em> fail. It would be interesting to get an opinion from someone involved in language internals on how PHP should behave. In either case, it&#8217;s very hard to understand why the first script doesn&#8217;t currently cause a notice or error to be thrown when the assignment fails.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2008/11/09/neat-php-tricks-assign-references-globals-workaround/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Stupid PHP Tricks: Illegal Variable Names</title>
		<link>http://www.otton.org/2008/08/21/stupid-php-tricks-illegal-variable-names/</link>
		<comments>http://www.otton.org/2008/08/21/stupid-php-tricks-illegal-variable-names/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 11:55:22 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[PHP Tricks]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=235</guid>
		<description><![CDATA[From the PHP manual:
A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
In other words, variable names can&#8217;t begin with a numeral. However there are a couple of ways to slip an illegal variable name into [...]]]></description>
			<content:encoded><![CDATA[<p>From the <a href="http://uk2.php.net/variables">PHP manual</a>:</p>
<blockquote><p>A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: <code>'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'</code></p></blockquote>
<p>In other words, variable names can&#8217;t begin with a numeral. However there are a couple of ways to slip an illegal variable name into the symbol table:</p>
<p><code>
<pre><span style="color: #0000ff;">$var</span> = <span style="color: #ff0000;">'1'</span>;
&nbsp;
$<span style="color: #0000ff;">$var</span> = <span style="color: #ff0000;">'hello world'</span>;</pre>
<p></code></p>
<p>or</p>
<p><code>
<pre>$<span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">&#125;</span> = <span style="color: #ff0000;">'hello world'</span>;</pre>
<p></code></p>
<p>You can prove to your own satisfaction that the variables really exist with PHP&#8217;s rather obscure <code>compact()</code> function:</p>
<p><code>
<pre><a href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/compact"><span style="color: #000066;">compact</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'1'</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p></code></p>
<p>Please note: if you use any of this in production code &#8211; and that includes <code>compact()</code> &#8211; I will come to your house and beat you with something heavy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2008/08/21/stupid-php-tricks-illegal-variable-names/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Neat PHP Tricks: Casting Arrays to Objects</title>
		<link>http://www.otton.org/2008/08/14/neat-php-tricks-casting-arrays-to-objects/</link>
		<comments>http://www.otton.org/2008/08/14/neat-php-tricks-casting-arrays-to-objects/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 18:00:07 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[PHP Tricks]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=210</guid>
		<description><![CDATA[Array notation is fine, but it can look a bit clunky when you&#8217;re working with complex structures. This is a fairly simple example, but I&#8217;m sure we&#8217;ve all dealt with worse:

&#160;
$clientChanges&#91;'deletes'&#93;&#91;$val&#91;'fkClient'&#93;&#93; = $val&#91;'Total'&#93;;

Casting the array to an object allows us to use object notation (-&#62;) and makes the code more readable:

&#160;
$val = &#40;object&#41; $val;
$clientChanges = [...]]]></description>
			<content:encoded><![CDATA[<p>Array notation is fine, but it can look a bit clunky when you&#8217;re working with complex structures. This is a fairly simple example, but I&#8217;m sure we&#8217;ve all dealt with worse:</p>
<p><code>
<pre>&nbsp;
<span style="color: #0000ff;">$clientChanges</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'deletes'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$val</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'fkClient'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'Total'</span><span style="color: #66cc66;">&#93;</span>;</pre>
<p></code></p>
<p>Casting the array to an object allows us to use object notation (<code>-&gt;</code>) and makes the code more readable:</p>
<p><code>
<pre>&nbsp;
<span style="color: #0000ff;">$val</span> = <span style="color: #66cc66;">&#40;</span>object<span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">$val</span>;
<span style="color: #0000ff;">$clientChanges</span> = <span style="color: #66cc66;">&#40;</span>object<span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">$clientChanges</span>;
&nbsp;
<span style="color: #0000ff;">$clientChanges</span>-&gt;<span style="color: #006600;">deletes</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$val</span>-&gt;<span style="color: #006600;">fkClient</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$val</span>-&gt;<span style="color: #006600;">Total</span>;</pre>
<p></code></p>
<p>You can even get away with using array functions on objects, as long as they&#8217;re just simple collections of properties:</p>
<p><code>
<pre>&nbsp;
<span style="color: #0000ff;">$o3</span> = <span style="color: #66cc66;">&#40;</span>object<span style="color: #66cc66;">&#41;</span> <a href="http://www.php.net/array_merge"><span style="color: #000066;">array_merge</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">$o1</span>, <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">$o2</span> <span style="color: #66cc66;">&#41;</span>;</pre>
<p></code></p>
<p>Of course, member functions won&#8217;t make it through this kind of mangling, but bizarrely, private variables do:</p>
<p><code>
<pre>&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> O
<span style="color: #66cc66;">&#123;</span>
    private <span style="color: #0000ff;">$a</span> = <span style="color: #cc66cc;">4</span>;
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$b</span> = <span style="color: #cc66cc;">5</span>;
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$c</span> = <span style="color: #cc66cc;">6</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$o</span> = <span style="color: #000000; font-weight: bold;">new</span> O<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$o</span> = <span style="color: #66cc66;">&#40;</span> object <span style="color: #66cc66;">&#41;</span> <a href="http://www.php.net/array_reverse"><span style="color: #000066;">array_reverse</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a> <span style="color: #66cc66;">&#41;</span><span style="color: #0000ff;">$o</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a href="http://www.php.net/var_dump"><span style="color: #000066;">var_dump</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$o</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * outputs:
 *
 * object(stdClass)#2 (3) {
 *     [&quot;c&quot;]=&gt; int(6)
 *     [&quot;b&quot;]=&gt; int(5)
 *     [&quot;a:private&quot;]=&gt; int(4)
 * }
 */</span></pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2008/08/14/neat-php-tricks-casting-arrays-to-objects/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Stupid PHP Tricks: (true == false)</title>
		<link>http://www.otton.org/2008/08/06/stupid-php-tricks-true-false-comparison/</link>
		<comments>http://www.otton.org/2008/08/06/stupid-php-tricks-true-false-comparison/#comments</comments>
		<pubDate>Wed, 06 Aug 2008 19:51:04 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[PHP Tricks]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=172</guid>
		<description><![CDATA[PHP is a weakly-typed language. By that I mean that variables are assigned values without regard to variable type, and implicit conversion at runtime sorts out any conflicts. PHP will happily let you compare a string and an integer, and if the string contains something unexpected, well, you should have paid more attention to data [...]]]></description>
			<content:encoded><![CDATA[<p>PHP is a weakly-typed language. By that I mean that variables are assigned values without regard to variable type, and implicit conversion at runtime sorts out any conflicts. PHP will happily let you compare a string and an integer, and if the string contains something unexpected, well, you should have paid more attention to data validation.</p>
<p>Given that the most important feature of PHP is the shallow learning curve, and that it is deployed in an environment where everything is a string, it&#8217;s (just) possible to argue that this was a good design decision. It allows you to do things like this:</p>
<p><code>
<pre>&nbsp;
<span style="color: #0000ff;">$n</span> = <span style="color: #ff0000;">"5"</span>;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #cc66cc;">3</span> + <span style="color: #0000ff;">$n</span>;</pre>
<p></code></p>
<p>without getting into lots of tiresome explicit conversion. But the implicit rules are sometimes a little too clever for their own good, and throw up oddities like this:</p>
<p><code>
<pre>&nbsp;
<span style="color: #0000ff;">$a</span> = <span style="color: #ff0000;">'string'</span>;
<span style="color: #0000ff;">$b</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$a</span> == <span style="color: #000000; font-weight: bold;">true</span> &amp;&amp; <span style="color: #0000ff;">$b</span> == <span style="color: #000000; font-weight: bold;">false</span> &amp;&amp; <span style="color: #0000ff;">$a</span> == <span style="color: #0000ff;">$b</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'universe broken'</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p></code></p>
<p>What&#8217;s going on here? Lets look at these three clauses in detail:</p>
<ul>
<li><code>( 'string' == true )</code> because any non-null string evaluates to <code>true</code> when compared with a boolean</li>
<li><code>( 0 == false )</code> because the integer <code>0</code> undergoes implicit conversion to boolean and evaluates to <code>false</code></li>
<li>Finally, <code>( 'string' == 0 )</code> because a string is silently promoted to integer when compared with an integer. If the string is the ASCII representation of a number (eg <code>"123"</code>), it is assigned that value. If it doesn&#8217;t contain a number, it is assigned the value 0. So our third clause evaluates as <code>true</code>. <em>Oops&#8230;</em></li>
</ul>
<p>I routinely use the triple-equals operator (identical) instead of the double-equals operator (equality) now, but it feels and looks like a hack to avoid someone else&#8217;s bad design decision biting me. PHP is chock-full of these little oddities.</p>
<p>If you want all the gory details, the <a href="http://uk.php.net/manual/en/types.comparisons.php">type comparison tables</a> are tucked deep inside the PHP Manual.</p>
<p>Update: <a href="http://news.ycombinator.com/item?id=269877">This post</a> over at ycombinator contains an excellent one-line summary of what I was getting at.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2008/08/06/stupid-php-tricks-true-false-comparison/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>
