note 39681 added to function.intval
| From: | tuxedobob at mac dot com | Date: | Sat, 07 Feb 2004 09:56:07 +0000 |
| Subject: | note 39681 added to function.intval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64753@lists.php.net to get a copy of this message | ||
Sometimes intval just won't cut it. For example if you want to use an unsigned 32-bit int and
need all 32 bits. Recently, I wrote a little script that took and integer and converted it to an IP
address. After realizing I couldn't just mod the whole thing, since the sign bit throws it off
(and compensating for that), we ran into a problem where if it was entered into a form, the value
somehow wasn't converted to an integer properly, at least not implicitly. The solution for
this, and the way I recommend converting a string to an integer, is:
$num = $num + 0;
and PHP will leave your number alone; it'll just know it's a number. Such is the fun of a
loosely-typed language. :)
----
Manual Page -- http://www.php.net/manual/en/function.intval.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39681
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39681&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39681&report=yes
Search -- http://master.php.net/manage/user-notes.php