<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Sparkling gems found in the official PHP documentation, mostly in the user-submitted comments. Not meant to pick on anyone, just to serve as a source of wonderment. Follow a bitter young curmudgeon on Twitter at @0xabad1dea. Avatar of elephant badge from http://www.adafruit.com/products/713</description><title>PHP Manual Masterpieces</title><generator>Tumblr (3.0; @phpmanualmasterpieces)</generator><link>http://phpmanualmasterpieces.tumblr.com/</link><item><title>Unsettling</title><description>&lt;p&gt;&lt;a href="http://php.net/manual/en/function.isset.php"&gt;&lt;code&gt;isset&lt;/code&gt; — Determine if a variable is set and is not &lt;code&gt;NULL&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We&amp;#8217;re not even through the first sentence of this manual page and I am already wondering why we&amp;#8217;re conflating two completely different definitions of a variable being &amp;#8220;set&amp;#8221; in one function. This function simultaneously tests whether the variable &lt;em&gt;name&lt;/em&gt; exists and whether the &lt;em&gt;value&lt;/em&gt; of the variable is non-null. In my personal opinion, those are two very different questions. I don&amp;#8217;t need a special snowflake function to test if the contents of a variable is null, but I do need one to inquire of the runtime if a variable name has been declared. (Insofar as PHP declares anything, which it doesn&amp;#8217;t, really.)&lt;/p&gt;

&lt;p&gt;Special snowflake functions is kind of the name of the game with PHP, though. We&amp;#8217;ll get back to that&amp;#8230;&lt;/p&gt;

&lt;p&gt;&amp;#8230; because &lt;code&gt;isset()&lt;/code&gt; isn&amp;#8217;t even a function. It&amp;#8217;s a godsdammed &lt;em&gt;token&lt;/em&gt;. A token. Parsed by the parser. This is like &lt;code&gt;sizeof()&lt;/code&gt; in C, except it &lt;em&gt;does&lt;/em&gt; run at runtime rather than returning a constant at the parsing stage, unless the PHP parser has solved the halting problem. The documentation calls it a &amp;#8220;language construct,&amp;#8221; implying, well, I don&amp;#8217;t know what that implies, because everything is a language construct. This particular quirk has already been covered in &lt;a href="http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/"&gt;the masterpiece of PHP hate blogging&lt;/a&gt;, but it bears some pondering. Note that &lt;code&gt;isset()&lt;/code&gt; is documented under the list of builtin PHP functions, and &amp;#8220;function&amp;#8221; is in the URL. It returns a bool, like a function would. The primary difference is that you get a &lt;em&gt;parse error&lt;/em&gt; if you pass an expression rather than a comma-delimited list of one or more variables.&lt;/p&gt;

&lt;p&gt;I think I have reconstructed the thought process behind this, and it&amp;#8217;s a good point: &amp;#8220;you know, PHP needs a way to inspect its environment at runtime to determine whether a particular variable has been instantiated. This is obviously perfectly suited for a built-in function. But wait! What if they call &lt;code&gt;isset($a + "b")&lt;/code&gt;, which is not a &lt;em&gt;bare&lt;/em&gt; variable?&amp;#8221;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php &amp;gt; $a = 2; if(isset($a+"b")) { echo "true\n"; } else { echo "false\n"; }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Parse error: parse error, expecting&lt;/code&gt;&amp;#8217;,&amp;#8221; or &lt;code&gt;')'' in php shell code on line 1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;My solution, assuming we can&amp;#8217;t interpolate the result of the expression and arrive at &amp;#8220;ab&amp;#8221;, would be to return false. It&amp;#8217;s not a variable. Therefore it is not set. Perhaps one would prefer it to return NULL, indicating the question has no answer. I&amp;#8217;m big on &lt;em&gt;generalizing&lt;/em&gt; and minimizing special snowflake cases. This is not, in my opinion, such a profound problem of engineering as to justify a massive exception to the concept of functions.&lt;/p&gt;

&lt;p&gt;Of course, &lt;code&gt;isset()&lt;/code&gt; is not the only pseudo-function implemented like this; &lt;code&gt;empty()&lt;/code&gt;, &lt;code&gt;unset()&lt;/code&gt;, and probably others exist. However:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php &amp;gt; const a = 2; if(defined('a')) { echo "true\n"; } else { echo "false\n"; }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php &amp;gt; const a = 2; if(defined('a'+'b')) { echo "true\n"; } else { echo "false\n"; }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;false&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Look at that! &lt;code&gt;defined()&lt;/code&gt; uses the &lt;em&gt;exact&lt;/em&gt; implementation I have suggested, but &lt;code&gt;isset()&lt;/code&gt; does not. You can throw anything into the argument of &lt;code&gt;defined()&lt;/code&gt; - some numbers, NULL, whatever - and it will return false without throwing a hissy fit. I note that &lt;code&gt;defined()&lt;/code&gt; takes a string argument, which is, I assume, the key implementation difference (please don&amp;#8217;t make me go look, I don&amp;#8217;t want to go back to the dark place). (Also, allow me to complain how these functions are vaguely named, which &lt;em&gt;requires&lt;/em&gt; that you look them up in documentation.) Seriously, what the hell is this, what are you doing with your life PHP, special casing all over the place and doing so &lt;em&gt;inconsistently&lt;/em&gt;, didn&amp;#8217;t your mother ever tell you - &lt;em&gt;deep breath&lt;/em&gt;, sorry about that. I get really worked up about programs sometimes.&lt;/p&gt;

&lt;p&gt;Anyway - remember what I was saying about special snowflake functions?&lt;/p&gt;

&lt;p&gt;The filter class &lt;a href="http://php.net/manual/en/function.filter-has-var.php"&gt;apparently has its own, slightly different implementation&lt;/a&gt;, which will only test the arrays used to store user inputs. &lt;em&gt;Why&lt;/em&gt; did they need to clutter up the runtime with a nearly identical implementation instead of &lt;code&gt;isset()&lt;/code&gt;? Perhaps they were annoyed by stupid, pointless parse errors and reimplementing under the standard library&amp;#8217;s nose was easier than getting it changed. I don&amp;#8217;t know. Perhaps there was a &lt;em&gt;really good reason&lt;/em&gt; they needed a separate &lt;code&gt;isset()&lt;/code&gt;, having to do with the &lt;em&gt;undocumented&lt;/em&gt; fact that it tests the &lt;em&gt;original&lt;/em&gt; input array sans any runtime changes. I don&amp;#8217;t know, because PHP never, ever documents the reason for &lt;em&gt;squat.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php &amp;gt; if(isset(NULL)) { echo "true\n"; } else { echo "false\n"; }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Parse error: parse error, expecting 'T_PAAMAYIM_NEKUDOTAYIM' in php shell code on line 1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;wat&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/52082199266</link><guid>http://phpmanualmasterpieces.tumblr.com/post/52082199266</guid><pubDate>Mon, 03 Jun 2013 16:51:56 -0400</pubDate><category>php</category><category>programming</category><category>rant</category><category>rage</category></item><item><title>The more you know ♒⭐</title><description>&lt;p&gt;Did you know that PHP has two entirely different syntax styles for if/else clauses? &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br/&gt;
if ($a &amp;gt; $b) {&lt;br/&gt;
    echo "a is bigger than b";&lt;br/&gt;
}&lt;br/&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br/&gt;
if($a &amp;gt; $b):&lt;br/&gt;
    echo $a." is greater than ".$b;&lt;br/&gt;
endif;&lt;br/&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;These are both lovely and sensible ways to delineate control structures. Having both in the same programming language, however, is madness.&lt;/p&gt;

&lt;p&gt;Did you know that mixing these two styles in close proximity can result in baffling errors, even if they&amp;#8217;re nested scopes that seem conceptually separate?&lt;/p&gt;

&lt;p&gt;Did you know that the brace style accepts both &amp;#8220;elseif&amp;#8221; and &amp;#8220;else if&amp;#8221;, but colon style only accepts &amp;#8220;elseif&amp;#8221;?&lt;/p&gt;

&lt;p&gt;&lt;a href="http://uk.php.net/manual/en/control-structures.elseif.php"&gt;I wouldn&amp;#8217;t lie to you. It really is that nonsensical.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Did you know this can also be done with loop structures? Did you know the &lt;a href="http://uk.php.net/manual/en/control-structures.alternative-syntax.php"&gt;documentation&lt;/a&gt; offers no rationale for why these two different syntaxes were implemented, complicating the parser and increasing opportunity for weird bugs because&amp;#8230;?&lt;/p&gt;

&lt;p&gt;The more you know, the more you&amp;#8217;re left to wonder.&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/44971646761</link><guid>http://phpmanualmasterpieces.tumblr.com/post/44971646761</guid><pubDate>Sat, 09 Mar 2013 17:23:38 -0500</pubDate><category>Php</category></item><item><title>I am so comically angry right now</title><description>&lt;p&gt;Hey uhh so strcmp() huh pretty simple I mean &lt;a href="http://php.net/manual/en/function.strcmp.php"&gt;what could be simpler&lt;/a&gt;: &lt;/p&gt;
&lt;p&gt;&lt;code&gt;Returns &amp;lt; 0 if str1 is less than str2; &amp;gt; 0 if str1 is greater than str2, and 0 if they are equal.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Pretty straightforward right how could this possibly be messed up it&amp;#8217;s freaking strcmp either the strings are equal or they ain&amp;#8217;t right&lt;/p&gt;
&lt;p&gt;&lt;a href="http://danuxx.blogspot.co.uk/2013/03/unauthorized-access-bypassing-php-strcmp.html"&gt;&lt;a href="http://danuxx.blogspot.co.uk/2013/03/unauthorized-access-bypassing-php-strcmp.html"&gt;http://danuxx.blogspot.co.uk/2013/03/unauthorized-access-bypassing-php-strcmp.html&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;GODS FREAKING DARN IT ALL TO HECK.&lt;/p&gt;
&lt;p&gt;This is why I hate you, PHP! This is why I consider you an unmitigated failure, a pox on the good name of programming! &lt;/p&gt;
&lt;p&gt;Dear language designer, pick one:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;A language with a very casual typing system;&lt;/li&gt;
&lt;li&gt;A language with standard API functions that cannot return an error indicating incompatible types were passed at runtime.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;NOT BOTH. YOU CAN&amp;#8217;T HAVE BOTH.&lt;/p&gt;
&lt;p&gt;When I shared this on Twitter, legions of people showed up to say it was the programmer&amp;#8217;s fault for using strcmp(). Riddle me this: is strcmp() part of the standard PHP API? Yes. Is it deprecated? No. Are there any caution notes in the official documentation? No. I don&amp;#8217;t care how hard you try to blame the programmer, this is fundamentally bad language design. It&amp;#8217;s a trap.&lt;/p&gt;
&lt;p&gt;You can easily cut yourself on the standard API of many languages. The difference is that languages like C are labeled &amp;#8220;sharp knife&amp;#8221; and PHP is labeled &amp;#8220;child-safe scissors.&amp;#8221; Cutting yourself should not be this easy.&lt;/p&gt;
&lt;p&gt;Dear sweet merciful angels I want to stab this language, twist the knife, yank out its still-beating heart and smash it against a rock.&lt;/p&gt;
&lt;p&gt;&lt;span&gt; (╯°□°）╯︵ ┻━┻  0xabad1dea OUT.&lt;/span&gt;&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/44634822304</link><guid>http://phpmanualmasterpieces.tumblr.com/post/44634822304</guid><pubDate>Tue, 05 Mar 2013 11:59:25 -0500</pubDate><category>php</category></item><item><title>when will you start doing some more articles ?</title><description>&lt;p&gt;Oh, sorry! I haven’t been forced to look at any PHP documentation for a while, can you tell? :)&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/37915468580</link><guid>http://phpmanualmasterpieces.tumblr.com/post/37915468580</guid><pubDate>Fri, 14 Dec 2012 12:20:10 -0500</pubDate></item><item><title>For once, I'm on Internet Explorer's side</title><description>&lt;p&gt;My &amp;#8220;pick a page at random and just read&amp;#8221; strategy continues to pay off.&lt;/p&gt;

&lt;p&gt;You just know that the documentation for the &lt;a href="http://www.php.net/manual/en/features.file-upload.php"&gt;upload features of PHP&lt;/a&gt; is going to have some really terrible implementations of handling file uploads in the comments. And it most certainly does.&lt;/p&gt;

&lt;p&gt;Simply not validating uploads, however, is so pedestrian. We want to go the extra mile, and the solution lies in a place you&amp;#8217;d never expect for a PHP wonder.&lt;/p&gt;

&lt;p&gt;From a comment of 2007 vintage:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;As far as I understand IE has his own MIME types based on the values stored in a registry. To locate this &amp;#8220;feature&amp;#8221; I spent a lot of time and was granted with a perfect headache. :)
  In my case I tried to upload a CSV file on a server and abort a script in case if the corresponding file isn&amp;#8217;t of a desired type. 
  And it work fine with Opera and stuck with IE. 
  So the workaround is that you should add this values in your windows registry (I have windows xp box and it works fine for me)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;pre&gt;
  [HKEY_CLASSES_ROOT\.csv]
  "Content Type"="application/vnd.ms-excel"
  @="Excel.CSV"
  [HKEY_CLASSES_ROOT\.csv\Excel.CSV]
  [HKEY_CLASSES_ROOT\.csv\Excel.CSV\ShellNew]

&lt;/pre&gt;

&lt;p&gt;I actually wasn&amp;#8217;t sure at first if the OP meant to modify the registry on the client or server side, because if I&amp;#8217;ve noticed a trend in PHP manual comments, it&amp;#8217;s that a surprisingly large portion of them are clearly running PHP on Windows servers. (Is this representative of PHP programmers in general, or only the kind that post comments on manual pages? More data needed.) After carefully reading it about a dozen times, however, I am &lt;a href="http://msdn.microsoft.com/en-us/library/ms775147(v=vs.85).aspx"&gt;&lt;em&gt;pretty&lt;/em&gt; sure&lt;/a&gt; they mean client side. Let that sink in.&lt;/p&gt;

&lt;p&gt;&amp;#8220;Want to use my website? Modify your Windows registry!&amp;#8221;&lt;/p&gt;

&lt;p&gt;How&amp;#8230; what&amp;#8230; I mean&amp;#8230; I know IE is a non-compliant pain to work with (especially back in 2007), but come on! Did it really never occur to this person that maybe they were not implementing things in the ideal way? Client-specified mimetypes, just like server-specified mimetypes, are completely arbitrary. The fact that IE doesn&amp;#8217;t come with a preset for CSV files should have been the dead giveaway that made this programmer pause and reconsider their solution.&lt;/p&gt;

&lt;p&gt;I bet they had some stupid case statement for the slightly different mimetypes returned by four different kinds of browsers, too, and that it broke every few months, and it &lt;em&gt;still&lt;/em&gt; never occurred to this person that their solution was not on solid ground. I simply can&amp;#8217;t have faith that it went down any other way.&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/33868345939</link><guid>http://phpmanualmasterpieces.tumblr.com/post/33868345939</guid><pubDate>Thu, 18 Oct 2012 20:43:57 -0400</pubDate><category>PHP</category></item><item><title>Drop the ball</title><description>&lt;p&gt;When your language manual straight-up has a page called &lt;a href="http://www.php.net/manual/en/language.types.type-juggling.php"&gt;Type Juggling&lt;/a&gt;, you&amp;#8217;re gonna have a bad time.&lt;/p&gt;

&lt;p&gt;Juggling is hard enough without adding knives and flaming brands to the mix, don&amp;#8217;t you think?&lt;/p&gt;

&lt;p&gt;From an oooold comment of the 2005 vintage:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In my much of my coding I have found it necessary to type-cast between objects of different class types.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;pre&gt;
function ClassTypeCast(&amp;amp;$obj,$class_type){
    if(class_exists($class_type,true)){
        $obj = unserialize(preg_replace"/^O:[0-9]+:\"[^\"]+\":/i", 
      "O:".strlen($class_type).":\"".$class_type."\":", serialize($obj)));
    }
}

&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s a bit dense, so let it sink in. This person is serializing an arbitrary class, using regex to &lt;em&gt;replace&lt;/em&gt; the serialized class name and its length, and unserializing to coerce PHP into some ghastly faux &amp;#8220;typecast&amp;#8221; of two different classes. I don&amp;#8217;t even &lt;em&gt;want&lt;/em&gt; to know what can go wrong here, because it can&amp;#8217;t be pretty. I want to slam the lid shut on this idea and lose the key down a hell pit.&lt;/p&gt;

&lt;p&gt;Just kidding, let&amp;#8217;s go Pandora on this box of badness.&lt;/p&gt;

&lt;pre&gt;
class MyClass {
    private $foo = null;

    function setString($str) {
        $this-&amp;gt;foo = $str; }
    
    function awful() {
        return "This is awful!"; }
}

class MyClass2 {
    private $foo2 = null;

    function setString2($str) {
        $this-&amp;gt;foo2 = $str; }
    
    function awful() {
        return "This is awesome!"; }
}

  $my1 = new MyClass();

  $my1-&amp;gt;setString("kill me now");

  ClassTypeCast($my1, "MyClass2");

  echo $my1-&amp;gt;awful() . "&amp;lt;br&amp;gt;";

  var_dump($my1);

&lt;/pre&gt;

&lt;pre&gt;
  This is awesome!
  object(MyClass2)#2 (2) { ["foo2:private"]=&amp;gt; NULL 
  ["foo:private"]=&amp;gt; string(11) "kill me now" }

&lt;/pre&gt;

&lt;p&gt;So as you can see, a quick test suggests that the resulting mangled class will have the data members of both classes. How in tarnation does that even &lt;em&gt;happen&lt;/em&gt;, PHP? When there is a naming conflict, it seems the new type takes precedence, but you get all these lingering hangers-on if the two types didn&amp;#8217;t have the exact same method and variable names. I bet there are all sorts of difficult-to-foresee side effects resulting from tampering with this nonsense.&lt;/p&gt;

&lt;p&gt;The only saving grace of this post is that they actually managed to lose a critical punctuation mark somewhere in there (an exercise for the reader), which causes it to fail to compile as written, which should stop the first tier of naive PHP programmers from cut-and-paste catastrophe.&lt;/p&gt;

&lt;p&gt;Put down the &lt;code&gt;serialize()&lt;/code&gt; and back away slowly.&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/33239208258</link><guid>http://phpmanualmasterpieces.tumblr.com/post/33239208258</guid><pubDate>Tue, 09 Oct 2012 13:39:07 -0400</pubDate><category>PHP</category></item><item><title>Layers of mistaken, like an ogre.</title><description>&lt;p&gt;From the comments section on the documentation of &lt;a href="http://php.net/manual/en/function.microtime.php"&gt;microtime&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Something I noticed:
  If you directly echo the time difference between start and the end you&amp;#8217;ll get a negative value most of the time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;pre&gt;
  &amp;lt;?php
  //This will produce in negative value:
  $start = microtime();
  for($i=100;$i&amp;gt;0;$i--){
  echo $i;
  }
  $end = microtime();

  echo 'Took:'.$end-$start.' seconds';
  ?&amp;gt;

&lt;/pre&gt;

&lt;p&gt;(I&amp;#8217;d like to note that tumblr does &lt;em&gt;not&lt;/em&gt; mess up the indentation, it comes to us exactly so.)&lt;/p&gt;

&lt;p&gt;Running this script exactly as given, I received this output:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-0.24061 seconds&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Oh wow, they&amp;#8217;re right! That&amp;#8217;s really wei - - - - Hang on. &lt;code&gt;echo 'Took:'.$end-$start.' seconds';&lt;/code&gt; Look at this line as long as you need to. Something looks different, right?&lt;/p&gt;

&lt;p&gt;Where did the &amp;#8220;Took:&amp;#8221; go in the output? Something&amp;#8217;s wrong. This coder has made a crucial mistake regarding the functionality of &lt;code&gt;microtime()&lt;/code&gt;, but before we even worry about that&amp;#8230;&lt;/p&gt;

&lt;pre&gt;
  $foo = 17;
  $bar = 15;
  echo "Concatenation".$foo."rocks!";
  echo "Concatenation".$foo;
  echo "Concatenation".$foo-$bar."rocks!"; // LOOK HERE
  echo "Concatenation".($foo-$bar)."rocks!";

  Concatenation17rocks!
  Concatenation17
  -15rocks! // AND HERE
  Concatenation2rocks!

&lt;/pre&gt;

&lt;p&gt;You know what? It took me a really long time to work out how PHP arrived at this bizarre result. Now I would have supposed that the concatenation operator would be either higher or lower than the subtraction operator, depending on the mood of the language designer, but to my surprise, in PHP they have the &lt;em&gt;same&lt;/em&gt; precedence, meaning we fall back on the left-to-right rule.This means we have:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(("Concatenation" . $foo) - $bar) . "rocks!";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;which is equivalent to:&lt;/p&gt;

&lt;p&gt;`(&amp;#8220;Concatenation17&amp;#8221; - 15) . &amp;#8220;rocks!&amp;#8221;;&lt;/p&gt;

&lt;p&gt;When you recall that a string cast to integer yields 0 if it doesn&amp;#8217;t begin with an ASCII-encoded number, suddenly this makes sense. 0 - 15 is in fact -15, giving us &amp;#8220;-15rocks!&amp;#8221;. That&amp;#8217;s &lt;em&gt;one&lt;/em&gt; problem down.&lt;/p&gt;

&lt;p&gt;The other problem, as I already hinted, is API misuse. &lt;code&gt;microtime()&lt;/code&gt; returns &lt;em&gt;a string&lt;/em&gt; that contains representations of &lt;em&gt;two values&lt;/em&gt; with a space in between, like this:&lt;code&gt;0.33485800 1349741452&lt;/code&gt;. Coercing that to a number will only run up until the space, meaning that subtracting two consecutive results is not meaningful if we&amp;#8217;ve crossed a seconds boundary in the meantime. We can fix this simply by changing the calls to &lt;code&gt;microtime(true)&lt;/code&gt; which, per the documentation, returns a normal float value of those two numbers added together. With that in mind&amp;#8230;&lt;/p&gt;

&lt;pre&gt;
  // I think you will find this altogether more what you wanted.
  $start = microtime(true);
  /* time waster goes here */
  $end = microtime(true);
  
  echo 'Took:'.($end-$start).' seconds&amp;lt;br&amp;gt;';
  // I don't like e-notation, personally
  // (and ironically this circumvents the precedence problem)
  printf ("Took %f seconds&amp;lt;br&amp;gt;",$end-$start);


  Took:2.4080276489258E-5 seconds
  Took 0.000024 seconds

&lt;/pre&gt;

&lt;p&gt;That looks an awful lot like a reasonable result. I declare victory over PHP yet again.&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/33198366857</link><guid>http://phpmanualmasterpieces.tumblr.com/post/33198366857</guid><pubDate>Mon, 08 Oct 2012 20:27:22 -0400</pubDate><category>PHP</category></item><item><title>Revolutionary input validation</title><description>&lt;p&gt;&lt;a href="http://www.php.net/manual/en/function.frenchtojd.php"&gt;&lt;code&gt;int frenchtojd ( int $month , int $day , int $year )&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Converts a date from the French Republican Calendar to a Julian Day Count.
  These routines only convert dates in years 1 through 14 (Gregorian dates 22 September 1792 through 22 September 1806). This more than covers the period when the calendar was in use.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I am so glad we have this in our standard library. This is such a critical use case of calendars on web sites.&lt;/p&gt;

&lt;p&gt;I searched Google Code and Github and I honestly could not find a single proper user of this function. All the hits were the string just appearing in geshi&amp;#8217;s syntax highlighting token tables. It&amp;#8217;s made it into some calendar conversion widgets because it just &lt;em&gt;happens&lt;/em&gt; to be there, I guess.&lt;/p&gt;

&lt;p&gt;Notice, however, that the documentation &lt;em&gt;does not say&lt;/em&gt; what happens if you pass in invalid data. Does it return a negative number? Raise an error condition of some kind? Return Napoleon&amp;#8217;s birthday? Who freaking knows! I&amp;#8217;d try to find out empirically, but my online PHP shell has actually &lt;em&gt;disabled&lt;/em&gt; this function for vague &amp;#8220;security reasons.&amp;#8221;&lt;/p&gt;

&lt;p&gt;So again, let&amp;#8217;s risk heart attack and check out &lt;a href="https://github.com/php/php-src/blob/master/ext/calendar/french.c"&gt;the source code&lt;/a&gt; which has not been modified in &lt;em&gt;eleven years.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Zero is returned when the input date is detected as invalid or out of the supported range. The return values will be &amp;gt; 0 for all valid, supported dates, but there are some invalid dates that will return a positive value. To verify that a date is valid, convert it to SDN and then back and compare with the original.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#8230; I&amp;#8217;m not even going to snark about that.&lt;/p&gt;

&lt;p&gt;Anticipating hate mail from exactly one history professor in France who&amp;#8217;s been using this for eleven years and  doesn&amp;#8217;t want to have to reimplement this.&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/33123216064</link><guid>http://phpmanualmasterpieces.tumblr.com/post/33123216064</guid><pubDate>Sun, 07 Oct 2012 20:05:17 -0400</pubDate><category>PHP</category></item><item><title>The documentation clearly says raptors.</title><description>&lt;p&gt;From the comment section of the documentation on &lt;a href="http://php.net/manual/en/language.exceptions.php"&gt;exceptions&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;To continue the execution code after throw new Exception, goto operator can be used, like this:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;pre&gt;
&amp;lt;?php
try {
    echo 'one';
    throw new Exception('-error-'); a:
    echo 'two';
} catch (Exception $e) {
    echo $e-&amp;gt;getMessage();
    goto a;
}
//output: one-error-two
?&amp;gt;

&lt;/pre&gt;

&lt;p&gt;AAAAAAAAA - hang on, when did PHP get goto? 5.3? All right then - AAAAAAH WHAT ARE YOU DOING.&lt;/p&gt;

&lt;p&gt;Now before you send me hatemail, I&amp;#8217;m an asm programmer and &lt;em&gt;I actually like goto&lt;/em&gt;, in the proper context. It&amp;#8217;s probably a good thing PHP didn&amp;#8217;t have goto back in the 4.2 days when I was learning, or I would have thought &amp;#8216;twas the neatest thing and used it everywhere. PHP&amp;#8217;s goto apparently has some restrictions on what sort of scopes you can jump between, which is a &lt;em&gt;good thing&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Apparently, jumping from a catch block into the middle of a try block is &lt;em&gt;not&lt;/em&gt; one of these restrictions.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m on vacation right now, and I&amp;#8217;ve been using this &lt;a href="http://writecodeonline.com/php/"&gt;online thingie&lt;/a&gt; to test the PHP snippets I&amp;#8217;ve been posting about. I just noticed it&amp;#8217;s actually still on PHP 5.2, so I haven&amp;#8217;t gotten to witness this executing - but I will take it on faith that it works.&lt;/p&gt;

&lt;p&gt;Try-catch is one of the most &lt;em&gt;structured&lt;/em&gt; concepts there is in programming, and goto is the sworn enemy of that. If you want to make sure that the next thing after an exceptable line in a try block always executes, &lt;em&gt;don&amp;#8217;t put it in the same try block!&lt;/em&gt; Just put it bare after the try/catch or, if it&amp;#8217;s also exceptable, in another try block. (I feel like &amp;#8220;exceptable&amp;#8221; may not be the scientific term.) Obviously the example in the comment is a proof-of-concept, but you are going to get yourself into trouble &lt;em&gt;so fast&lt;/em&gt; like this, unknown commenter. Do not trifle with the gods of program control flow, for they are subtle and quick to segfault. (Normally I wouldn&amp;#8217;t worry about segfaulting in an interpreted language, but, well, PHP. It happens.)&lt;/p&gt;

&lt;p&gt;Let me put on my security auditor hat. When you use goto to defeat control flow, you are making it a &lt;em&gt;lot&lt;/em&gt; freaking harder to verify the correctness of your program. Terrible, wicked bugs will hide in the nooks and crannies of your supposed cunning to devour you. More importantly, your auditor will go looking for her murderin&amp;#8217; axe that she keeps in that closet you&amp;#8217;re not allowed to open.&lt;/p&gt;

&lt;p&gt;And if you&amp;#8217;re &lt;em&gt;still&lt;/em&gt; not convinced, if you check the &lt;a href="http://php.net/manual/en/control-structures.goto.php"&gt;documentation on goto&lt;/a&gt;, it &lt;em&gt;clearly&lt;/em&gt; says beneath the examples that if you actually use this feature, you will be eaten by raptors. (Who approved this for mainline?!)&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/33091353115</link><guid>http://phpmanualmasterpieces.tumblr.com/post/33091353115</guid><pubDate>Sun, 07 Oct 2012 12:49:00 -0400</pubDate><category>PHP</category></item><item><title>Two's complewhat</title><description>&lt;p&gt;I continued reading the comments on the bizarre &lt;a href="http://php.net/manual/en/function.dechex.php"&gt;dechex&lt;/a&gt; conversion function which works on an &amp;#8220;unsigned int&amp;#8221; type that doesn&amp;#8217;t actually exist in PHP. It says that negative signed numbers shall be treated as unsigned.&lt;/p&gt;

&lt;p&gt;Someone took this to mean it creates incorrect results for negative numbers and tried to roll their own which &lt;em&gt;could&lt;/em&gt; produce the negative hex representations of negative integers. Ignore the fact that it doesn&amp;#8217;t prepend &lt;code&gt;0x&lt;/code&gt; to the output as neither does &lt;code&gt;dechex()&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;
function dec_to_hex($dec) 
{ 
    $sign = ""; // suppress errors 
    if( $dec &amp;lt; 0){ $sign = "-"; $dec = abs($dec); } 

/* ... an array-index based algorithm goes here */
    
    return $sign . $h; 
} 

&lt;/pre&gt;

&lt;p&gt;If you pass 256, you get the output 100. If you pass -256, you get the output&amp;#8230; -100. With the literal unary operator.&lt;/p&gt;

&lt;p&gt;This opens an interesting question about what maketh a negative number. In a purely abstract mathematical sense, slapping a negative sign on a positive number is &amp;#8220;correct&amp;#8221; in any base. But this is computer programming, and hexadecimal is special, because we use it to map directly to literal bit values whereas decimal is an abstraction.&lt;/p&gt;

&lt;p&gt;Assuming we add the &lt;code&gt;0x&lt;/code&gt; prefix (after the unary dash) to make real hexadecimal number tokens out of these results, we &lt;em&gt;can&lt;/em&gt; do math like this: &lt;code&gt;257 + -0x100&lt;/code&gt; returns 1 as expected. This is because PHP is taking the unary operator and doing a two&amp;#8217;s complement negation of 0x100 at runtime. -0x100 is &lt;em&gt;not the actual hex representation of -256, it is an expression that evaluates to it.&lt;/em&gt; It&amp;#8217;s like returning the string  &amp;#8220;2 + 3&amp;#8221; and saying you&amp;#8217;ve returned &amp;#8220;5&amp;#8221;. Only&amp;#8230; sort of.&lt;/p&gt;

&lt;p&gt;The actual negation of 0x100 is 0xFFFFFF00 on 32-bit or 0xFFFFFFFFFFFFFF00 on 64-bit (note the lack of negative signs). If you are unsure where all those F&amp;#8217;s came from, check out &lt;a href="http://en.wikipedia.org/wiki/Two's_complement"&gt;two&amp;#8217;s complement representation&lt;/a&gt;. Honestly, this is a very complex subject and I don&amp;#8217;t blame beginner programmers, or ones who have only ever dealt with scripting languages, for falling down on this.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s the thing, though. If you call &lt;code&gt;dechex(-256)&lt;/code&gt;, you &lt;em&gt;will&lt;/em&gt; get 0xFFFFFF00, the correct result, even though the documentation explicitly warns that it coerces input to be unsigned. When you understand why, you have achieved binary representation nirvana.&lt;/p&gt;

&lt;p&gt;I did not understand hexadecimal, binary, signed and unsigned, and casting - all concepts which PHP &lt;em&gt;exposes&lt;/em&gt; but does not handle very gracefully -  until PHP stopped being my only programming language. And that&amp;#8217;s your PSA for the day.&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/33051383652</link><guid>http://phpmanualmasterpieces.tumblr.com/post/33051383652</guid><pubDate>Sat, 06 Oct 2012 22:07:11 -0400</pubDate><category>PHP</category></item><item><title>Junior discovers type coercion</title><description>&lt;p&gt;This is a post where I really must emphasize I don&amp;#8217;t mean to pick on the original commenter as this looks &lt;em&gt;very&lt;/em&gt; much like a typical beginner mistake crossed with a &lt;em&gt;very&lt;/em&gt; bad API. The sad thing is that things like this just promote the idea thatt programming is strange, mysterious, and inconsistent.&lt;/p&gt;

&lt;p&gt;From a comment on the documentation for &lt;a href="http://php.net/manual/en/language.types.integer.php"&gt;integers&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;To force the correct usage of 32-bit unsigned integer in some functions, just add &amp;#8216;+0&amp;#8217;  just before processing them.&lt;/p&gt;
  
  &lt;p&gt;for example&lt;br/&gt;&lt;code&gt;echo(dechex("2724838310"));&lt;/code&gt;&lt;br/&gt;
  will print &amp;#8216;7FFFFFFF&amp;#8217;&lt;br/&gt;
  but it should print &amp;#8216;A269BBA6&amp;#8217;&lt;/p&gt;
  
  &lt;p&gt;When adding &amp;#8216;+0&amp;#8217; php will handle the 32bit unsigned integer&lt;br/&gt;
  correctly&lt;br/&gt;&lt;code&gt;echo(dechex("2724838310"+0));&lt;/code&gt;&lt;br/&gt;
  will print &amp;#8216;A269BBA6&amp;#8217;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The naive ingenuity of this &amp;#8220;solution&amp;#8221; is surprising. How did they ever arrive at this workaround? What was the thought process there? How did going so wrong go so right?&lt;/p&gt;

&lt;p&gt;Ultimately I think the blame for confusion goes to &lt;a href="http://php.net/manual/en/function.dechex.php"&gt;dechex&lt;/a&gt; and related functions for randomly taking &lt;em&gt;unsigned&lt;/em&gt; arguments when all PHP ints are &lt;em&gt;signed&lt;/em&gt;. The function is prototyped as taking an int, but if you actually want to take advantage of the unsignedness above normal int limits you have to pass a float, and if you&amp;#8217;re insisting on starting from textual data then you have to cast it to float yourself (lest it coerce to int). That is what this person is managing to accomplish in a roundabout way by adding zero.&lt;/p&gt;

&lt;p&gt;Why in tarnation do these functions accept &amp;#8220;unsigned ints&amp;#8221;, a datatype that the language is &lt;em&gt;not even aware of&lt;/em&gt;?&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/33038956335</link><guid>http://phpmanualmasterpieces.tumblr.com/post/33038956335</guid><pubDate>Sat, 06 Oct 2012 18:53:00 -0400</pubDate><category>PHP</category></item><item><title>Two quotes that make me sad.</title><description>&lt;p&gt;From the comment section of &lt;a href="http://php.net/manual/en/function.unserialize.php"&gt;unserialize&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Apparently, unserialize is really picky about anyone messing with the serial string. Just spent an hour debugging why unserialize wasn&amp;#8217;t working on a serial string stored in a database where, per client requirement, all inserted data is strtoupper&amp;#8217;d.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I hate your client and, depending on whether or not you as a professional programmer &lt;em&gt;expected&lt;/em&gt; unserialize() to work in the face of bytes being changed, I may also hate you.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Be aware that if useing serialize/unserialize in a serverfarm with both 32bit and 64bit servers you can get unexpected results.
  Ex: if you serialize an integer with value of 2147483648 on a 64bit system and then unserialize it on a 32bit system you will get the value -2147483648 instead. This is because an integer on 32bit cannot be above 2147483647 so it wraps.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That sounds like the sort of unintended result that maybe should be &lt;em&gt;freaking documented&lt;/em&gt;, and I don&amp;#8217;t mean in a user-submitted comment waaaay down at the bottom.&lt;/p&gt;

&lt;p&gt;Bonus: people complaining that you cannot unserialize complex objects for which you do not already have the definition declared, because opening arbitrary mystery meat objects is definitely something you should be doing.&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/33035925694</link><guid>http://phpmanualmasterpieces.tumblr.com/post/33035925694</guid><pubDate>Sat, 06 Oct 2012 18:07:26 -0400</pubDate></item><item><title>When does one byte equal four kilobytes?</title><description>&lt;p&gt;From the official documentation of &lt;a href="http://us3.php.net/manual/en/function.ob-start.php"&gt;ob_start&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;If the optional parameter chunk_size is passed, the buffer will be flushed after any output call which causes the buffer&amp;#8217;s length to equal or exceed chunk_size. The default value 0 means that the output function will only be called when the output buffer is closed.&lt;/p&gt;
  
  &lt;p&gt;&lt;strong&gt;Prior to PHP 5.4.0, the value 1 was a special case value that set the chunk size to 4096 bytes.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I can imagine the meeting where that decision was originally made. &amp;#8220;So a lot of people want to use four kilobytes as their chunk size, but wow, that&amp;#8217;s a lot of digits. Is there some other, shorter number we can use instead of 4096? How about 1? No-one would ever want a chunk size of one, right?&amp;#8221;&lt;/p&gt;

&lt;p&gt;Actually there is probably some far more horrifying reason, something like some stupid legacy system measuring in 4096-byte chunks. However, here&amp;#8217;s the thing: I carefully read over the March 2012&amp;#160;&lt;a href="http://www.php.net/ChangeLog-5.php#5.4.0"&gt;5.4.0 changelog&lt;/a&gt; (the version where the documentation claims this was &amp;#8220;fixed&amp;#8221; for 1 to mean 1) but I can&amp;#8217;t find any reference to it (but I did get some new gray hairs from other bugs listed). I searched the bugs database and found an &lt;em&gt;open&lt;/em&gt; report from 2008&amp;#160;&lt;a href="https://bugs.php.net/bug.php?id=45391"&gt;pleading&lt;/a&gt; for 1 to equal 1. In November 2011 it was &lt;a href="https://bugs.php.net/bug.php?id=60243"&gt;documented&lt;/a&gt; on a similar report that the fix was coming in 5.4.0.&lt;/p&gt;

&lt;p&gt;Oh, hahaha of course, it&amp;#8217;s not in the changelog, it&amp;#8217;s in the &lt;a href="http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/UPGRADING?view=markup&amp;amp;pathrev=319050"&gt;upgrade notes&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;But I still can&amp;#8217;t find the reason that 1 == 4096 in the first place. Let&amp;#8217;s resort to checking&amp;#8230; the actual source code.&lt;/p&gt;

&lt;p&gt;Choosing an arbitrarily ancient commit of &lt;a href="https://github.com/php/php-src/blob/f7a8499806c4fa7994fdff3655be5018f71f8b99/main/output.c#L139"&gt;output.c&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;
if (chunk_size==1) {
            chunk_size = 4096;
        }

&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s it. No explanation in the code. There is no reason. There never was.&lt;/p&gt;

&lt;p&gt;War is peace. 1 is 4096. We have always been at war with Goodcodeia.&lt;/p&gt;

&lt;p&gt;EDIT: @benheaslewood is better than me at reading changelogs and found the original reason, which makes this look stupider than ever.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/php/php-src/commit/4356932dfe142bcd4f8ede540c8a7415b175445d"&gt;original bug&lt;/a&gt; was that 1 could be passed in from a boolean configuration setting, which would cause 1 to be divided by 2, which in integer math yields 0, which caused an infinite loop in some allocator function.&lt;/p&gt;

&lt;p&gt;Some people might solve this by &lt;em&gt;not dividing 1 by 2&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;But no, PHP of ten years ago solved it by first declaring 1 to be 0, and then later &lt;a href="https://github.com/php/php-src/commit/d4bba6d158b75128c590dc6de7ec07ccbdf2a7c9"&gt;changing their minds&lt;/a&gt; and declaring 1 to be 4096 (not even a constant that represents 4096, just a bare literal). &lt;em&gt;I cannot figure out why they did this.&lt;/em&gt; Is there a reason block_size and chunk_size cannot be the same? I don&amp;#8217;t know, I give up.&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/33023415508</link><guid>http://phpmanualmasterpieces.tumblr.com/post/33023415508</guid><pubDate>Sat, 06 Oct 2012 15:06:00 -0400</pubDate></item><item><title>Here's my int, so cast it maybe</title><description>&lt;p&gt;From a comment on the documentation of &lt;a href="http://www.php.net/manual/en/language.variables.php"&gt;variables&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;
function CAST_TO_INT($var, $min = FALSE, $max = FALSE)
{
    $var = is_int($var) ? $var : (int)(is_scalar($var) ? $var : 0);
    if ($min !== FALSE &amp;amp;&amp;amp; $var &amp;lt; $min)
        return $min;
    elseif($max !== FALSE &amp;amp;&amp;amp; $var &amp;gt; $max)
        return $max;
    return $var;
        
}
&lt;/pre&gt;

&lt;p&gt;I am truly struggling to imagine what the point of this &amp;#8220;utility&amp;#8221; function is. It &amp;#8220;casts to int&amp;#8221; by&amp;#8230; using the built-in means of casting to int. If PHP can&amp;#8217;t figure how to make an int out of what you handed it, it gives you a zero, as this goes out of its way to do explicitly.&lt;/p&gt;

&lt;p&gt;So that just leaves these mysterious min/max clamping parameters, which have nothing to do with casting. This function should have been named CLAMP_INT_TO_RANGE. Note, however, that &lt;code&gt;$min&lt;/code&gt; and &lt;code&gt;$max&lt;/code&gt; do &lt;em&gt;not&lt;/em&gt; have to be integers! That means that the call &lt;code&gt;CAST_TO_INT(6, "foo", "bar");&lt;/code&gt; &lt;em&gt;will return &amp;#8220;bar&amp;#8221;&lt;/em&gt;. Casting to int, except when suddenly string.&lt;/p&gt;

&lt;p&gt;(Comparing an int to a string will cast the string to an int by the normal rules, meaning both &amp;#8220;foo&amp;#8221; and &amp;#8220;bar&amp;#8221; are 0 for the purposes of comparison against 6 as they do not represent an ASCII-encoded integer. That is, when you&amp;#8217;re using &amp;#8220;==&amp;#8221; and not &amp;#8220;===&amp;#8221;. As a young programmer I completely failed to understand what the distinction there is.)&lt;/p&gt;</description><link>http://phpmanualmasterpieces.tumblr.com/post/32987674597</link><guid>http://phpmanualmasterpieces.tumblr.com/post/32987674597</guid><pubDate>Sat, 06 Oct 2012 00:21:00 -0400</pubDate><category>casting</category><category>comment</category><category>PHP</category></item></channel></rss>
