<?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>Wed, 14 Dec 2011 10:13:11 +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>The method invocation operator, and expressions, in PHP</title>
		<link>http://www.databasesandlife.com/the-method-invocation-operator-and-expressions-in-php/</link>
		<comments>http://www.databasesandlife.com/the-method-invocation-operator-and-expressions-in-php/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 14:01:48 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=826</guid>
		<description><![CDATA[PHP is a weird language. $x = new Foo(); print $x-&#62;bar(); works fine as you&#8217;d expect. but print new Foo()-&#62;bar(); print (new Foo())-&#62;bar(); all do not work. I do not understand how you can build an expression parser, where the code above works, and the code below does not. It&#8217;s almost as if &#8220;$x = [...]]]></description>
			<content:encoded><![CDATA[<p>PHP is a weird language.</p>
<pre>
$x = new Foo();
print $x-&gt;bar();
</pre>
<p>works fine as you&#8217;d expect. but</p>
<pre>
print new Foo()-&gt;bar();
print (new Foo())-&gt;bar();
</pre>
<p>all do not work.</p>
<p>I do not understand how you can build an expression parser, where the code above works, and the code below does not. It&#8217;s almost as if &#8220;$x = new XXX(xxx)&#8221; a separate case searched for by the parser?</p>
<p>I mean if you have an expression parser capable of handling brackets, i.e. (1+2)*4, and you have the expression (new Foo()) and you have the concept of $object->method() then how can it possibly not work?</p>
<pre>
$ <strong>cat -n x.php</strong>
     1  &lt;?php
     2
     3  class Foo {
     4     function bar() { return 4; }
     5  }
     6
     7  $foo = new Foo();
     8  print $foo-&gt;bar();
     9
    10  print (new Foo())-&gt;bar();

$ <strong>php -e x.php</strong>
Parse error: syntax error, unexpected T_OBJECT_OPERATOR
in /home/adrian/x.php on line 10
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/the-method-invocation-operator-and-expressions-in-php/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 [...]]]></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 [...]]]></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 [...]]]></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 [...]]]></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>

