|
Posted by Wayne on 02/03/06 22:11
On 3 Feb 2006 09:03:34 -0800, "comp.lang.php"
<phillip.s.powell@gmail.com> wrote:
>I'm involved in a rather nasty debate involving a strange issue
>(whereby the exasperated tell me to RTFM even after my having done so),
>where this is insanely possible:
>
>[PHP]
>print_r(is_int('1')); // PRINTS NOTHING
>print_r(strlen((int)1)); // PRINTS '1'
>[/PHP]
>
>Now I understand that in PHP, everything scalar is a string
*bzzt* wrong. This is where you are getting lost.
>and can take on the role of an integer, or a boolean or..
Everything has a type -- so integers are integers and strings are
strings and booleans are booleans. However, PHP will automatically
cast scalars to different types as appropriate for a function or
operator.
So in this case:
var_dump(is_int('1')) // prints false, it's a string
var_dump(is_int(1)) // prints true, is an int
var_dump(is_string('1')) // prints true, is a string
var_dump(is_string(false)) // prints false, is a boolean
echo strlen(144) // prints 3 because it casts the number to a string
The is_* functions return the type of the variable -- they do not care
about the contents themselves. So it doesn't care that it's a string
containing an integer.
You can use the is_numeric() function to see if a string value
contains a number.
PHP will automatically cast in cases where you do something like this:
$x = '12' + '16'; // $x will contain 28.
And you can manually cast values yourself:
$x = (integer)'12'; // $x will contain an integer of 12
$x = (string)8l // $x will contain the string '8'
>when it seems to me that I have it right based on my basic
>understanding the definition of "types"?
As I said, you're basic definition is wrong. Not all scalar values in
PHP are strings.
Navigation:
[Reply to this message]
|