<?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>Databases and Life &#187; PHP</title>
	<atom:link href="http://www.databasesandlife.com/category/coding/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.databasesandlife.com</link>
	<description>Adrian Smith's blog</description>
	<lastBuildDate>Mon, 14 Jun 2010 09:41:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The special variable &#8220;_&#8221;</title>
		<link>http://www.databasesandlife.com/the-underscore-variable/</link>
		<comments>http://www.databasesandlife.com/the-underscore-variable/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 14:05:39 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=479</guid>
		<description><![CDATA[Reading this blog post, Destructuring binds in Ruby just now reminded me of a feature I love about Prolog which I wish would make it into other languages.
Firstly, I love assigning a list to a list of lvalues i.e. variables; this is possible in both PHP and Perl which I use regularly; and no doubt [...]]]></description>
			<content:encoded><![CDATA[<p>Reading this blog post, <a href="http://citizen428.net/archives/427-Destructuring-binds-in-Ruby.html">Destructuring binds in Ruby</a> just now reminded me of a feature I love about Prolog which I wish would make it into other languages.</p>
<p>Firstly, I love assigning a list to a list of <a href="http://cplus.about.com/od/glossar1/g/lvalue.htm">lvalues</a> i.e. variables; this is possible in both PHP and Perl which I use regularly; and no doubt many other languages. (But not Java: why not!?)</p>
<pre>
    ($a, $b) =      ($b, $a);  // Perl
list($a, $b) = array($b, $a);  // PHP
</pre>
<p>PHP, as always, wins in inelegance, having the left side syntactically different to the right side. While it&#8217;s obviously the case that a list of values and a list of lvalues are technically different, I don&#8217;t think this difference should be expressed in the syntax.</p>
<p>I mean, in most languages you write e.g. $foo=4 and $bar=$foo; in both those cases you write $foo but yet they do something different (lvalue and rvalue); given that you write them the same there i think the same should apply to lists.</p>
<p>But I digress &#8211; What I want to mention is using &#8220;underscore&#8221; to mean &#8220;any variable&#8221;. I first saw this in Prolog.</p>
<p>For example, imagine you have to implement an interface (e.g. in Java), it requires you to write a function taking two parameters, but one of the parameters you don&#8217;t care about. Wouldn&#8217;t it be nice to write</p>
<pre>
interface ExistingApi {
   public void createObject(String name, Object otherInformation);
}

class MyInstanceOfTheApi {
   public void createObject(String name, _) {
      ...
   }
}
</pre>
<p>i.e. this shows clearly you do not care about the second parameter. </p>
<p>In current Java (and all languages I program including Perl, PHP) you have to give all variables a name even if you don’t use them, either in function definitions or in “assign to a list” scenarios mentioned above. It is then left as an exercise to the reader to determine if these variables are used or not, and indeed an exercise to the writer to name the variable they are never going to use.</p>
<p>I mean yes, technically you can actually just call variables “_” (or “$_” (except in Perl where “$_” already means something)) but that would then be a coding convention as opposed to a language feature, and who knows if the coding convention is actually used correctly by a programmer. (If “_” is a variable there’s nothing stopping someone from using its value.)</p>
<p>And then you have the problem if you have two variables you don&#8217;t care about, you can&#8217;t call them both &#8220;_&#8221;.</p>
<p>By the way, <a href="http://books.google.com/books?id=VjHk2Cjrti8C&#038;printsec=frontcover&#038;dq=Programming+in+Prolog&#038;source=bl&#038;ots=IHQtLmvJqC&#038;sig=RFhnlEzs_Nu_GkwTQD_Z3som5Ds#v=onepage&#038;q&#038;f=false">Programming in Prolog</a> is an excellent book.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/the-underscore-variable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP infinite recursion</title>
		<link>http://www.databasesandlife.com/php-infinite-recursion/</link>
		<comments>http://www.databasesandlife.com/php-infinite-recursion/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 12:50:11 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=388</guid>
		<description><![CDATA[What can I say? How about &#8220;toy language&#8221;?

$ php -r 'function foo() { foo(); } foo();'
Segmentation fault

I&#8217;m not saying that infinite recursion is a good idea, but during development it can happen by accident, and I don&#8217;t expect such a simple error to crash the PHP interpreter! (Also it took me about 20 minutes to [...]]]></description>
			<content:encoded><![CDATA[<p>What can I say? How about &#8220;toy language&#8221;?</p>
<pre>
$ php -r 'function foo() { foo(); } foo();'
Segmentation fault
</pre>
<p>I&#8217;m not saying that infinite recursion is a good idea, but during development it can happen by accident, and I don&#8217;t expect such a simple error to crash the PHP interpreter! (Also it took me about 20 minutes to debug this problem, as I had no idea where it happened, nor indeed what the problem was..)</p>
<p><i>PHP 5.2.6 on Linux 2.6.26 Debian</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/php-infinite-recursion/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Never close PHP class files with the &#8220;?&gt;&#8221; tag</title>
		<link>http://www.databasesandlife.com/never-close-php-class-files-with-the-tag/</link>
		<comments>http://www.databasesandlife.com/never-close-php-class-files-with-the-tag/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 09:47:38 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=348</guid>
		<description><![CDATA[When developing PHP, a front-end PHP file will include other files: classes,  utilities, etc.
When writing those class files, one also needs to use the &#60;?php tag at the start of the file, otherwise PHP will simply take the text and output it unchanged to the browser. (PHP&#8217;s assumption is that it sits in a [...]]]></description>
			<content:encoded><![CDATA[<p>When developing PHP, a front-end PHP file will include other files: classes,  utilities, etc.</p>
<p>When writing those class files, one also needs to use the <strong>&lt;?php</strong> tag at the start of the file, otherwise PHP will simply take the text and output it unchanged to the browser. (PHP&#8217;s assumption is that it sits in a web page, with probably more markup than code, so by default characters in the source code get copied one-to-one to the browser and the <strong>&lt;?php</strong>&#8230;<strong>?&gt;</strong> tags are necessary to introduce PHP to the &#8220;exceptional circumstance&#8221; that one might actually want to program some PHP.)</p>
<p>If one must open the class source file with <strong>&lt;?php</strong> then it would seem to make aesthetic sense to close it with <strong>?&gt;</strong>. However, there are no negative  side-effects if one does not close the tag, plus one very negative side-effect  if one does close it.</p>
<p>We performed a minor release a while ago, after which the display of generated PDF files no longer worked. Yet the minor release had nothing to do with the section of code  that produced PDFs. What sort of weird <a href="http://en.wikipedia.org/wiki/Action_at_a_distance_(computer_science)">action-at-a-distance</a> could possibly be happening here?</p>
<p>The reason was that one class file in the minor release had a blank line after the <strong>?&gt;</strong> tag. This was impossible to spot in the text editor. The blank line was printed to the browser, which was also invisible in nearly all of the site, as HTML ignores blank lines. PDFs probably do as well (I haven&#8217;t checked) but the problem wasn&#8217;t with the content. As HTTP response content is streamed to the browser (as opposed to being collected first and then sent to the browser at the end of the request), HTTP headers can only be set before the first byte of output has been produced by the software. As the blank line in the class source file consituted  content, and the source file was (necessarily) parsed before the code could be executed, the HTTP header &#8220;Content-Type: text/pdf&#8221; couldn&#8217;t be sent, and various errors about headers not being able to be sent, combined with the binary source  of the PDF, arrived at the user&#8217;s screen.</p>
<p>So given there  are no disadvantages, and one particulary weird source of bugs can be removed, I  would say one should never end PHP files with <strong>?&gt;</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/never-close-php-class-files-with-the-tag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysqli_affected_rows</title>
		<link>http://www.databasesandlife.com/mysqli_affected_rows/</link>
		<comments>http://www.databasesandlife.com/mysqli_affected_rows/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 13:37:13 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=314</guid>
		<description><![CDATA[Recently I programmed the following screen in PHP:

The user logs in
The user has a subscription
The subscription has a number of states (&#8220;terminate&#8221;, &#8220;auto-extend&#8221;, ..)
There is a screen allowing the user to change this state
The screen is a set of radio buttons &#8211; each radio button relates to one state
The user clicks on the radio-button representing [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I programmed the following screen in PHP:</p>
<ul class="tight">
<li>The user logs in</li>
<li>The user has a subscription</li>
<li>The subscription has a number of states (&#8220;terminate&#8221;, &#8220;auto-extend&#8221;, ..)</li>
<li>There is a screen allowing the user to change this state</li>
<li>The screen is a set of radio buttons &#8211; each radio button relates to one state</li>
<li>The user clicks on the radio-button representing the state they wish, clicks &#8220;ok&#8221;, and the new state gets saved to the database</li>
</ul>
<p>Not rocket science eh? Well, unbelievably my implementation of the above had a bug. How on earth was that possible?</p>
<p>The bug was the following: If you changed the state, everything worked fine. But if you chose the same state as is already selected, an Exception gets thrown.</p>
<p>Initially I suspected a simple coding mistake. When I looked at the code, everything looked right. I had used the following &#8220;algorithm&#8221;:</p>
<ul class="tight">
<li>Update the &#8220;subscription&#8221; row using SQL</li>
<li>Check the result of the SQL statement, that exactly 1 row was updated (in case e.g. id referenced a non-existing subscription, which would be an error)</li>
</ul>
<p>I used the PHP function <a href="http://www.phpbuilder.com/manual/en/function.mysqli-affected-rows.php">mysqli_affected_rows</a> for that and unbelievably that has the following functionality: it only returns the number of <em>changed</em> rows i.e. the number of rows:</p>
<ul class="tight">
<li>Matching the where clause, <em>and</em></li>
<li>Currently having values different to those values being written to the row.</li>
</ul>
<p>I can&#8217;t imagine a case where one would want to know that. I couldn&#8217;t find any function to return the number of rows matching, independent of if the values were changed or not. (The older version <a href="http://at2.php.net/mysqli_affected_rows">mysql_affected_rows</a> exhibits the identical functionality.)</p>
<p>So I had to write the following function:</p>
<pre>/**
 * Returns the number of rows which matched the WHERE
 * clause on the last UPDATE statement. This is not the
 * same as mysqli_affected_rows, which only returns the
 * number of changed rows.
 */
public static function DbUpdatedRows() {
    $link = self::DbGetLink();  // mysqli object
    $info = mysqli_info($link);
    if (preg_match('/Rows matched: (\d+) +Changed/',
            $info, $matches))
        return $matches[1];
    throw new Exception("DbUpdatedRows called although ".
        "it doesn't look like an UPDATE was the ".
        "last statement: mysqli_info returned '$info'");
}</pre>
<p>I&#8217;ve just checked, and in InnoDB inside a transaction, it&#8217;s good to see that (as with Oracle) write-locks are indeed placed on all matched rows not just updated rows.</p>
<p>And don&#8217;t get me started on using DB-specific function calls (i.e. functions named mysql_x) as opposed to using a DB-abstraction layer like DBI in Perl, JDBC in Java, etc. Nor why I&#8217;m using PHP or MySQL in the first place.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/mysqli_affected_rows/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Next PHP uselessness of the day</title>
		<link>http://www.databasesandlife.com/next-php-uselessness-of-the-day/</link>
		<comments>http://www.databasesandlife.com/next-php-uselessness-of-the-day/#comments</comments>
		<pubDate>Mon, 10 Dec 2007 22:06:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=181</guid>
		<description><![CDATA[There is the option &#8220;php -l&#8221; which checks the validity of a source file. Obviously it doesn&#8217;t do a wonderful job as it doesn&#8217;t detect misspelt variable or method names; but I suppose it&#8217;s better than nothing.
So I apply this recursively to all the files under a certain directory. For reasons I won&#8217;t go into [...]]]></description>
			<content:encoded><![CDATA[<p>There is the option &#8220;php -l&#8221; which checks the validity of a source file. Obviously it doesn&#8217;t do a wonderful job as it doesn&#8217;t detect misspelt variable or method names; but I suppose it&#8217;s better than nothing.</p>
<p>So I apply this recursively to all the files under a certain directory. For reasons I won&#8217;t go into here there are Postscript fonts checked into the source directory. To this files, &#8220;php -l&#8221; outputs:</p>
<pre>
No syntax errors detected in ./pdflib/fonts/php_Times-Italic.afm
</pre>
<p>I assert &#8220;php -l&#8221; is not very useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/next-php-uselessness-of-the-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unbelievable PHP limitation of the day</title>
		<link>http://www.databasesandlife.com/unbelievable-php-limitation-of-the-day/</link>
		<comments>http://www.databasesandlife.com/unbelievable-php-limitation-of-the-day/#comments</comments>
		<pubDate>Mon, 10 Dec 2007 16:27:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=182</guid>
		<description><![CDATA[If one defines a class with the member variable:

protected static $bytes = 12582912;

then that&#8217;s fine. However if one defines it as:

protected static $bytes  = 12*1024*1024; // 12 MB

then that gives a compile error:

Parse error: syntax error, unexpected '*', expecting ',' or ';'

I know of no other language that I&#8217;ve ever programmed (i.e. including BASIC, [...]]]></description>
			<content:encoded><![CDATA[<p>If one defines a class with the member variable:</p>
<pre>
protected static $bytes = 12582912;
</pre>
<p>then that&#8217;s fine. However if one defines it as:</p>
<pre>
protected static $bytes  = 12*1024*1024; // 12 MB
</pre>
<p>then that gives a compile error:</p>
<pre>
Parse error: syntax error, unexpected '*', expecting ',' or ';'
</pre>
<p>I know of no other language that I&#8217;ve ever programmed (i.e. including BASIC, and C) where you can write a value, but you can&#8217;t write an expression.</p>
<p>How broken is that!</p>
<p>Putting spaces around the *, or adding brackets around the whole expression, does not help.</p>
<p>(PHP 5.2.0)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/unbelievable-php-limitation-of-the-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
