I think I've turned into a programming curmudgeon

I hate dynamically typed languages.

Perl, PHP, Python, Lua, the whole lot.

Why?

Because programmers who program in such languages are muddleheaded thinkers [1] who hate to declare variable types because they're too lazy to think and find it fun to “organically grow” their code bases.

It doesn't help that PHP (the current focus for my rage right now) is the ultimate in “scripting languages du jour,” where even minor releases are incompatible with each other.

I'm installing a PHP app (what that particular package is doesn't matter) but it's having problems connecting to the database (PostgreSQL [2] in this case, and yes, it's one of the few PHP apps that actually acknowledge the existance of a database other than MySQL [3]). So, I log into phpPgAdmin [4] to make sure the appropriate PostgreSQL user can access the appropriate PstgreSQL database, only what do I get?

**Warning:** Invalid argument supplied for foreach() in **/var/www/html/db/postgres/privileges.php** on line **187**

Alright … what's the line in question?

>
```
foreach ($privileges as $v) {
...
}
```

$privileges isn't mistyped (a common problem in a langauge where you don't have to declare your variables). I check some documentation [5] and yes, that's the correct syntax, but comments from the peanut gallery are going on and on about foreach breaking on copies of data or something; stuff that isn't reassuring.

So I rewrite the code (remember now, I'm trying to install SomeRandomPHPApp, I am not trying to debug phpPgAdmin):

>
```
reset($privileges); // XXX spc
while(list(,$v) = each($privileges)) { // XXX spc
//foreach ($privileges as $v) { // XXX spc
...
}
```

And try [DELETED-my call again-DELETED] reloading the page:

**Warning:** reset() [function.reset]: Passed variable is not an array or object in **/var/www/html/db/postgres/privileges.php** on line **187**
**Warning:** Variable passed to each() is not an array or object in **/var/www/html/db/postgres/privileges.php** on line **188**

Okay, what exactly do I have? $privileges is obviously not an array. Okay, more debugging (“I'm not even supposed to be here today [6]!”).

>
```
$privileges = $data->getPrivileges($object, $_REQUEST['subject']);
echo "TYEP: " . gettype($privileges) . " : $privileges"; // XXX spc
```

And what do I get?

TYEP: integer
**Warning:** reset() [function.reset]: Passed variable is not an array or object in **/var/www/html/db/postgres/privileges.php** on line **188**
**Warning:** Variable passed to each() is not an array or object in **/var/www/html/db/postgres/privileges.php** on line **189**

Oh lovely. getPrivileges() is now returning an integer, and the sizeof() function of PHP is returning a value larger than 0 (since the next thing done right after calling getPrivileges() is a call to sizeof() to see if getPrivileges() returned anything of any appreciable size) because an integer has a size, don't you know?

Oh, so what's the actual value of $privileges?

-3

Probably some internal error result deep from the bowels of PHP.

And not an array, like the programmer who originally wrote this crap expected.

Had there been some real typechecking going on I wouldn't be subjected to this type of error and the programmer would have been forced to think about the situation.

Hmm … actually, now that I'm reading up on sizeof() [7], I think the blame for this is defintely with the crack-addled developers of PHP. sizeof() is an alias for count(), which in part, reads:

Returns the number of elements in var, which is typically an array [8], since anything else will have one element.

“PHP: count [9]”

“Typically.” Oh, I love that bit.

Okay, so if sizeof() (aka (also known as) count()) will return a count of 1 for non-arrays and not signal any type of error because you “typically” use this on arrays, then why does foreach() barf on a non-array? Couldn't it just loop once? If sizeof() will treat a non-array as an array of one, why can't foreach()?

I mean, isn't that the purpose of a dynamically typed language? To act reasonably in any given situation? To not care if something is an array, list, vector, scalar, hashtable, or carrier pidgeon?

Hmmm … on second thought, that still doesn't absolve the programmer of phpPgAdmin—since there still is the issue of getPrivileges() returning an integer instead of an array of arrays (at least, that's what the code seems to be expecting).

And that still leaves my original problem currently unsolved.

[1] /boston/2006/06/05.1

[2] http://www.postgresql.org/

[3] http://www.mysql.com/

[4] http://phppgadmin.sourceforge.net/

[5] http://www.php.net/manual/en/control-structures.foreach.php

[6] http://www.viewaskew.com/interviews/briano.html

[7] http://www.php.net/manual/en/function.sizeof.php

[8] http://www.php.net/manual/en/language.types.array.php

[9] http://www.php.net/manual/en/function.count.php

Gemini Mention this post

Contact the author