Re: Data Types Problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



By definition, all HTTP data is of type 'string' because that's the
ONLY data type HTTP supports.

PHP will cheerfully typecast it to whatever it needs to be later, but
when it first comes in from the GET (or POST or COOKIE or whatever)
data, it's going to start its life as a string, no matter how it
looks.

If you *know* it should be integer, typecast it as you get it:

$foo = (int) $_GET['foo'];

Do this to a) self-document your code, b) foil a ton of web-bots and
nasty people trying to cram the wrong kind of data into your
application to break/abuse it.


On Mon, March 12, 2007 2:26 am, Arno Coetzee wrote:
> Hi Guys
>
> I seem to have a problem with data types in php
>
> when executing the following script , i do not get the desired output.
>
> i a pass url parameter (message) to the script.
>
> if i pass a numeric value (1) , the script accepts it as '01' and vice
> versa.
> the same with 0 and '00'
>
> here is the script:
>
> <?php
>     $theMessage = $_REQUEST['message'];
>
>     echo "theMessage : '" . $theMessage . "'\n";
>
>     if ($theMessage == "01")
>     {
>         echo "message : '01'";
>     }else if ($theMessage == "00")
>     {
>         echo "message : '00'";
>     }else if (is_numeric($theMessage))
>     {
>          echo "Numeric Input : " . $theMessage;
>     }else
>     {
>         echo "Invalid Input";
>     }
> ?>
>
> i presume that i am not interpreting the data types it should be
> interpreted.
>
> can anyone please help
>
> --
> Arno Coetzee
> Flash Media Group
> Developer
> Mobile : 27 82 693 6180
> Office : 27 12 430 7597
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux