Speaking of grief in PHP, I've been starting to code seriously in PHP for all of a few months now, and I'm learning a lot as I go, but man, the pain, the grief PHP inflicts upon the unwary
Having read The PHP Singularity - and then STILL deciding to go that path, I thought I was well prepared for what lay ahead. How wrong I was! PHP has an uncanny knack of kicking you in the bejezel when you least expect it.
For example. I learnt that all arrays in PHP are associative. Ok, I can deal with that. array[0] is really just array["0"] or something like that. It's really quite flexible. So I was a little disturbed when I went looking for how to determine if a given array is associative or not. I mean, it can't be that hard right!? (I've got to stop saying that!) It wasn't just that there was no "correct" way to determine the answer. It wasn't just that every thread I stumbled across that discussed it was suggesting bizarre and crazy ways to determine the answer. I mean, having much Perl experience I'm familiar with the "many ways to skin a cat" paradigm. But all the answers I found just seemed so... ugly!
Look, I'm not the worlds greatest programmer. Far from it. But still, I like my solutions to be both simple and stylish, if I can help it. That PHP has a built-in function for sooo many things, but not this... Well, what else to expect from a committee-designed weakly-typed language I guess. If I don't like it, I can always bugger off I suppose. *sigh*
In the end I settled on this:
function isAssoc($array) { return array_values($array) === $array ? FALSE : TRUE; }
Also, what's with these damn error messages - or lack thereof! It's bad enough that there's no way to enforce type checking and variable declaration (No, really, I want to declare ALL my variables before I use them. Honest!) similar to Perl's "use strict". But how hard does PHP make you work just to see ANY error messages!? And even then on certain versions (not that old, I swear) php -l wouldn't even tell me what line number my error was on, simply saying:
syntax error in file
Syntax error somewhere in your file. Good luck with that. Gee, thanks PHP.
Another thing that really bugs me is the way PHP will sometimes tell me the variable I'm trying to use has not been initialised before use, but at othertimes wont. What is with that!? Ok. This might only apply to (slightly) older versions of PHP, but unfortunately our PHP server at work cannot be upgraded for reason too complicated to explain here :(
On the other hand, PHP gets upset if I try to apply the ++ operator to an array, even if I've initialised the array as empty.
BREAKING NEWS: I've discovered the array_fill() function. So now I can initialise my arrays with:
$a = array_fill($start, $size, $initial_value);and dodge all those nasty "undefined offset" errors which so many PHP programmers don't seem to mind scattered all through their logs.
Array Insert Order
Ok. Associate arrays. Great. Index arrays. Still work just fine. But arrays that remember the order of insertion? Please, does that has to be built-in to the language?? For the love of programming, why? > So:
$a[5] = 'five'; $a[1] = 'one'; $a[8] = 'eight'; $a[2] = 'two; echo implode(',', $a);will print:
five, one, eight, two
IS THAT REALLY NECESSARY??
Ok. Before I learnt to use array_fill() - which inserts my initial values in order anyway - I found that I could get my elements back into index-order with a ksort(). Eg. With the example above, if, before the "echo" statement, I add:
ksort($a);I will then get my elements printed in the expected order: one, two, five, eight.
But of course I wont need to as I'll be using array_fill() so as to avoid any "undefined offset" errors.
No comments:
Post a Comment