Tagged with 'php'

Lazy typing comedy

So, does zero equal 'principal' ?

That's the question that's been driving me nuts today. More accurately, why does PHP think zero and 'principal' are the same thing?

I have this array of people at a school. Some of them are teachers, some of them are principals. In PHP, you're allowed to mix array indices (most other languages would require a Hash), so teachers get inserted with simple numerical indices, the principal gets a text index, "principal".

With me so far?

Now I want to print out a list of all the teachers. Everybody that isn't a principal, in other words. So, everybody that does not have an index of "principal".

<?php foreach ($school['people'] as $i=>$contact) {
if ($i != 'principal') {
?>

Except, when you do this, it strips out the principal and the first person.

The fix I found looks like this:

<?php foreach ($school['people'] as $i=>$contact) {
if ($i !== 'principal') {
?>

In PHP, "!==" means "is the same type and same value as". I think PHP was converting 0 to false, before the comparison as "false is not 'principal'".

I hate this stupid pseudo-language. Grrr!

PermalinkComments (7)

Meet the new blog

I've been wanting to re-do the blog in Rails, since well before the recent re-design. It was one of those "Oh, I want feature X and Y, and Liz really wants to do Z, but..…

Read more…PermalinkPost a comment

Some Rails "Gotchas" for PHP Devs

Rails is weird, sometimes, for someone used to PHP. Things that have consistently stumped me longer than they deserve to:

"render(:text=>some_obj)"…

Read more…PermalinkComments (2)

A Practical Example: PHP vs. RoR

I had a situation come up yesterday that, I think, illustrates what I like about Ruby on Rails particularly splendidly.

The Project: Revamp of a client's Web…

Read more…PermalinkPost a comment