Re: Data Types Problem

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

 



Hello,
           Becuase PHP don't convert "01" string to integer 1. In the PHP 
help file :

PHP Help for type casting

When a string is evaluated as a numeric value, the resulting value and type 
are determined as follows.

The string will evaluate as a float if it contains any of the characters 
'.', 'e', or 'E'. Otherwise, it will evaluate as an integer.

The value is given by the initial portion of the string. If the string 
starts with valid numeric data, this will be the value used. Otherwise, the 
value will be 0 (zero). Valid numeric data is an optional sign, followed by 
one or more digits (optionally containing a decimal point), followed by an 
optional exponent. The exponent is an 'e' or 'E' followed by one or more 
digits.

<?php
$foo = 1 + "10.5";                // $foo is float (11.5)
$foo = 1 + "-1.3e3";              // $foo is float (-1299)
$foo = 1 + "bob-1.3e3";           // $foo is integer (1)
$foo = 1 + "bob3";                // $foo is integer (1)
$foo = 1 + "10 Small Pigs";       // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1;          // $foo is float (11)
$foo = "10.0 pigs " + 1.0;        // $foo is float (11)
?>

        You can use = = =  comparision operator for check your variable type 
and variable value at the same time:) $a = = = $b Identical TRUE if $a is 
equal to $b, and they are of the same type. (introduced in PHP 4)



<?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";
    }
?>



-- 
Haydar TUNA
Republic Of Turkey - Ministry of National Education
Education Technology Department Ankara / TURKEY
Web: http://www.haydartuna.net


"Arno Coetzee" <arno@xxxxxxxxxxxxxxxx> wrote in message 
news:45F50095.3080505@xxxxxxxxxxxxxxxxxxx
> 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


[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