Note to anyone else who might do the dumb thing I did. When you're handling GET/POST input in PHP and wan to know if a value is numeric, don't use is_int() because it'll lie to you. What you want is is_numeric(). Of course, the is_int() documentation specifically warns you about this, but it doesn't do much good if you read right past it.
I was *this* close to walking over to Rasmus or Andrei and asking for a sanity check, but I decided to re-read the docs one more time. Heh. There's an hour or so I'll never get back.
Of course, this *is* the first PHP work I've done in quite a while. You don't want to know how many times I tried to declare a variable like: my $foo, thinking it was Perl. I think that's finally out of my system.
Note: Coments may be off-line for a bit. I'm being DoS'd by some comment script kiddies right now. Retards.
Posted by jzawodn at January 29, 2004 11:50 AM
Yep, no kidding. But what do you expect from a mysql'er? That database has laughable data coercion.
I agree with you on the my $foo, when I first started using PHP instead of Perl which was a while ago now, I would do that all of the time!
Donny
Thanks Larry, for your valuable input. Oh wait, no, that was completely worthless and is nothing but flamebait considering what you said is not only off topic but also makes little sense. Good try though.
LOL, can you imagine being able to just WALK over to Rasmus or Andrei for PHP help? Me thinks they'd probably just tell you to RTFM!! ;P
Hell, if ONLY I could just walk over to either one of them for help! :)
A similar one that has burned me is defined() vs. isset(). defined() is for something goofy like contansts and isset() is what you really want to check if a variable is defined. Gah!
I sometimes use my declaration in javascript too. Doesn't help that I often have the same validation algos in js as well as perl, and if you've been at it for a few hours, the syntax hilighting in vim makes it look very similar.
Believe it or not, I had the exact same problem a week ago which brought me to the function description, where it was clearly stated :)
And I think we share the same background. I learnt Perl first, and when I started PHP I was always thinking "man why can't I just do it as in Perl?".
The one thing that has kept me with php so consistently over the last 2 years is that /everything/ is addressed in the online docs. If it's not in the official function description or feature descriptions, the well-moderated boards ensure that each and every OS/db/phpversion anomelie is addressed. Working alone w/o even another engineer (nonetheless Rasmus) I have found the php docs to make up for that 10 fold.
Thank you php community, 10x!
That’s why, in such cases, I always try to first cast the user data to the int type, and then check whether it isn’t a zero. It’s not the same thing, of course, and not every time things can be simplified this way, but when they can I feel somehow safer knowing I *do* have a real integer (no pun intended) now.
Funny thing about that - andrei was actually the one that added the is_numeric() function. :)
There's also the ctype_digit() function that checks whether or not a string contains all numeric characters. For example:
$n = '1234.5';
var_dump(is_numeric($n)); // returns bool(true)
var_dump(ctype_digit($n)); // returns bool(false)