The whole point of loose typing

I don't know John Lim, but he linked to a Livejournal post where someone complained about their expectations being dashed when trying various comparisons.


$a = 0;
$b = "eggs";
$c = "spam";

print ($a == $b) ? "a == b\n" : "a != b\n";
print ($b == $c) ? "b == c\n" : "b != c\n";
print ($a == $c) ? "a == c\n" : "a != c\n";
print ($a == $d) ? "a == d\n" : "a != d\n";
print ($b == $d) ? "b == d\n" : "b != d\n";
print ($c == $d) ? "c == d\n" : "c != d\n";


% php equality.php
a == b
b != c
a == c
a == d
b != d
c != d

They proceed to be pretty confused by what could be perceived as confusing behavior. PHP does some type juggling in the background.

First, let's examine what happens when we cast various common values as booleans. There are two type specifiers because I specify each item as the type and the value, and then cast that.

/* Plain old true and false values */
(boolean) (boolean) true === (boolean) true
(boolean) (boolean) false === (boolean) false

/* A NULL, which is a special value */
(boolean) (NULL) NULL === (boolean) false

/* An empty string */
(boolean) (string) '' === (boolean) false

/* The numbers zero and one */
(boolean) (integer) 0 === (boolean) false
(boolean) (integer) 1 === (boolean) true

/* Other non-zero numbers (this behavior is just like C) */
(boolean) (integer) -1 === (boolean) true
(boolean) (integer) 100 === (boolean) true

/* The number zero in string form: the string zero */
(boolean) (string) '0' === (boolean) false

/* An empty array */
(boolean) (array) array() === (boolean) false;

/* Non-empty arrays */
(boolean) (array) array(0) === (boolean) true;
(boolean) (array) array(1) === (boolean) true;
(boolean) (array) array(0, 1) === (boolean) true;

/* A non-empty string */
(boolean) (string) 'in the hay' === (boolean) true

/* A tricky non-empty string */
(boolean) (string) 'false' === (boolean) true

/* Really tricky non-empty strings
* I might actually take issue with these,
* because they are really sneaky, but
* what is (probably) happening is that
* first the strings get converted into integers
* and then booleans.
(boolean) (string) '0 is awesome' === (boolean) true
(boolean) (string) ' 0' === (boolean) true

/* That notion seems to be help up by this test: */
(boolean) (string) '1' === (boolean) true

/* Fortunately, it is not completely ridiculous */
(boolean) (string) 'awesome is 0' === (boolean) true

So what is going on in that "crazy" example? One of the things to point out is that non-empty non-numeric strings are being compared to the number zero. My guess is that the zero is being juggled into a string since the string cannot be juggled into a number.


$a = 0;
$b = "eggs";
$c = "spam";

(string) $a is '0';
(string) $d is 0 (because it is not set)
(string) $d is '' (because it is not set)

print ($a == $b) ? "(string) a == (string) b\n" : "(string) a != (string) b\n";
print ($b == $c) ? "(string) b == (string) c\n" : "(string) b != (string) c\n";
print ($a == $c) ? "(string) a == (string) c\n" : "(string) a != (string) c\n";
print ($a == $d) ? "(int) a == (int) d\n" : "(int) a != (int) d\n";
print ($b == $d) ? "(string) b == (string) d\n" : "(string) b != (string) d\n";
print ($c == $d) ? "(string) c == (string) d\n" : "(string) c != (string) d\n";


% php equality.php
(string) a == (string) b
(string) b != (string) c
(string) a == (string) c
(int) a == (int) d
(string) b != (string) d
(string) c != (string) d

I wouldn't mind a more strongly typed language sometimes, and I might have made different design decisions, but they were made for a reason.

Another site on the internet lamented the fact that strpos() returns a false on failure and a 0 if the string match happens at the zero index, complaining that the required code is not very clear.

if (false !== strpos('haystack', 'needle')) {
echo 'Found it!';

Their comment was that this (the C syntax) would be oh-so-much-better:

if (-1 < strpos('haystack', 'needle')) {
echo 'Found it!';

Wow. That's so much clearer! I suppose that the meat of their problem was that one has to use the !== operator instead of the != operator to check for false. Yes, strpos is kind of a hassle that way, but if it really bothers you, it is trivially easy to write a wrapper function.

/* Note that I am Elliot-Smithing the argument order */
function needle_in_the_hay($needle, $haystack) {
return (false !== strpos($haystack, $needle);

The real problem here is that strpos in C is for finding a single character in another string and is being abused in php to find whole strings in other strings. Better to pick a new name for that purpose. Even so, ultimately it's just an excuse to whine about PHP. It's not a perfect language, but it is what it is.

You could really make the complaint that there should be a String class with methods and stuff like that. I would accept that complaint... and then ask why you aren't writing that class. You know you want to!

Oh, php has a nice chart of comparisons.

I Still Won't Use IIS

The constant joke about the Microsoft Web Development Technology Summit was that they were spiking the Kool-Aid® with mind control drugs. There is direct evidence to the contrary:

  1. There was no Kool-Aid®

  2. The drugs were in the bacon

    They were Beggin'® Strips

  3. I love bacon

  4. Microsoft is totally amazing and great for open source and doesn't have anything to prove to anyone so why would they use mind control anyway? Yay Microsoft!

With that bit of sass out of the way, I'll get to the meat of the issue: apparently Microsoft finally supports a fastcgi implementation for IIS. PHP via ISAPI has issues (thread safety? stability?). PHP via regular cgi has other issues (loading the entire PHP stack with every request?), but now you can finally run PHP at a decent rate on your IIS server. This is all predicated on the notion that you have access to an IIS server. Then again, if you don't, you can pluck one from the bounty of the internet via a buffer overflow exploit

Yes, I love jokes from 2001


Perhaps it's unfair to take potshots at Microsoft. Anything that they can do to open up their software to interoperation is a good thing. If I could do web development on my desktop machine with really good native Windows apps I would rejoice, because I like the interface. However, that's about the only thing that I like about Windows: the crude and sometimes brutal interface. I like the bleak squareness of Win2k, it's the Eastern Europe of operating systems.

Why would anyone choose IIS? That's a tougher question to answer. I've worked in a pile of computing environments: mainframes, windows, and various flavors of *NIX. Until this job at the University, I wasn't writing software -- I was trapped in userland doing actual work. There I had no choice -- I just had to screen scrape, write VBA, or sneak little perl scripts in to hammer data into compliance.

Therein lies my prejudice: I want to be able to sneak in those perl scripts, and I don't want to have to rewrite them for each place that I use them. I don't want to deal with things like the registry, which requires extra tools to back up.

sudo scp -r /etc user@newbox:/oldbox/

The registry is a great idea, but it represents the Microsoft Way: buy in completely to our philosophy or get bent. They finally wrote a new shell for Windows (the Powershell), which sounds completely cool and interesting, but isn't what I wanted: a native bash. I have cygwin, but if I want a shell, I ssh to my linux box. On second thought, I don't have cygwin, because when I built a new computer I figured that it would be easier to reinstall Windows on a new drive than figure out how to make Old Windows talk to New Hardware.

Maybe that's more complicated than it needs to be, and maybe it is not complicated enough. All I know is that I completely understand the urge to stand on the roof and scream in Swedish "Microsoftjävlar

Yes, I love incomprehensibly paraphrasing Danish miniserieses. Did you see Riget? h0bbel said that "jævler" is the Norwegian equivalent of the Swedish word "jävlar."


Nonetheless, I envy Visual Studio, and my experiences with MSSQL Server were outstanding. I even enjoyed the heck out of Microsoft Access (when not using it as a database engine, but rather a front end to MSSQL Server). I understand that ActiveDirectory is pretty cool. Once you buy in to one of these technologies, it can be easier to just buy the next piece that already knows how to talk to your first piece than to build some sort of fragile and magical connector. You've already got a box running IIS that handles some custom .NET app that fulfills your business requirements and want to extend it to handle a bit of PHP on the side? Now you can do so with a clear conscience.

I dunno, fastcgi seems kind of a non-announcement. How about letting me maximize my shell window, mount an NFS drive, read and write my ext3 or murderfs


drive, or have normal line endings

Yes, I know about DOS2UNIX. I have had to write my own DOS2UNIX script because I couldn't find one for the box I was on -- but shouldn't we stop the problem at its source?


Detecting Cycles in Tree Structures with PHP

My Gallery recently had some serious database issues. I had to write some code outside of the Gallery proper to look for database errors. When I went to look for cycles in the tree, I didn't find any handy algorithms out in the wild of the internet, so I wrote something.

That produces this output:

Found cycle in 1:2,2:3,3:1
Found cycle in 1:2,2:3,4:1,3:4
Found cycle in 1:2,2:1

I hope that helps whomever might be searching for this very algorithm. Please note that this does not detect duplicate children or missing parents.

Speakers Five: John VanDyk and Matt Westgate

These guys wrote the book on Drupal.

Maybe I'm just running out of steam, or it's just hard to summarize a summary. There have been hints that they will send a copy of their presentation, in which case I will post it (or link to it) for you.

Speaker Four: Dave Fetterman

Dave Fetterman is from facebook -- I think I mentioned him in passing before. The presentation thus far has been a shotgun intro to the facebook development process.

Exciting words used so far:

  • Sizzle

  • Sugar

Topics that he covered, at about this speed:

  • Facebook Platform Technical Capabilities

  • API: The Social Web Service

  • FQL: Facebook-centric query language

  • FBML: Server-Side display - augmented

  • Facebook Data Store API

  • Facebook JavaScript

Microsoft Popfly WTF?

And we're done. Whew.

 1 2 3  5 Next →