469,592 Members | 2,041 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

WTF? String = Integer in PHP? Strlen() in integers WORKS? HELP!

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 and can
take on the role of an integer, or a boolean or.. whatever it's
configured to look like. Why is it that I'm so "way off" in this
thread, when it seems to me that I have it right based on my basic
understanding the definition of "types"?

Phil

http://phpbuilder.com/board/showthread.php?t=10316949

Feb 3 '06 #1
6 6504
d
"comp.lang.php" <ph**************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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 and can
take on the role of an integer, or a boolean or.. whatever it's
configured to look like. Why is it that I'm so "way off" in this
thread, when it seems to me that I have it right based on my basic
understanding the definition of "types"?

Phil

http://phpbuilder.com/board/showthread.php?t=10316949


If you try that with var_dump, you get a false and a 1, which is what you'd
expect... Am I missing something here?? :)
Feb 3 '06 #2
comp.lang.php 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 and can
take on the role of an integer, or a boolean or.. whatever it's
configured to look like. Why is it that I'm so "way off" in this
thread, when it seems to me that I have it right based on my basic
understanding the definition of "types"?

Phil

http://phpbuilder.com/board/showthread.php?t=10316949


strlen() expects a string as a parameter. because of the "typeless"
nature of PHP, when something other than a string is sent, PHP will
automatically "cast" it to a string (like using the strval() function).
Therefore, when the length is calculated, it is operating on the
equivalent to $int = strval($int) instead of the actual integer.

The same type of thing happens when using expressions:

('1' == TRUE) will result in a bool(true). This is because the lowest
common type between string and bool is bool (the simplest of all).
Therefore, the string is cast to a boolean val, then the comparison is made.

Consider var_dump('my string'==1). This returns bool(false) because the
string is cast to an integer (the simplest common type) which is 0.

In constrast, var_dump('1 string'==1) return bool(true) because the
intval('1 string') === 1
The type Juggling section of the manual is about the best reference you
can use for this kind of thing, but IMO it isn't nearly as complete as
it could be for a novice level explanation.

http://us2.php.net/manual/en/languag...e-juggling.php

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Feb 3 '06 #3
On 3 Feb 2006 09:03:34 -0800, "comp.lang.php"
<ph**************@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.

Feb 3 '06 #4
Wayne wrote:
On 3 Feb 2006 09:03:34 -0800, "comp.lang.php"
<ph**************@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.


That phrasing is a little confusing...
integer, float, string and boolean are *all* scalar types. (The only 4
scalar types supported by PHP.)

Supported types that are not scalar are:
* the 2 compound types array and object
* the 2 special types NULL and resource.

PHP will automatically cast to the lowest common type in expressions.
Therefore comparing an array to an object will cause both to be cast to
a string first.
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.
To test for an integer from a string, use something like:

if(is_numeric(string) && intval($string)==$string){
// $string contains a string representation of an integer
}else{
// not an integer (it's a float)
}
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.


....but all integer, float, string and boolean values are scalar. ;)

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Feb 3 '06 #5
Justin Koivisto wrote:
Wayne wrote:
On 3 Feb 2006 09:03:34 -0800, "comp.lang.php"
<ph**************@gmail.com> wrote:
[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.

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.


That phrasing is a little confusing...
integer, float, string and boolean are *all* scalar types. (The only 4
scalar types supported by PHP.)

Supported types that are not scalar are:
* the 2 compound types array and object
* the 2 special types NULL and resource.


*bzzt* wrong

PHP references are scalar - but in most instances dereferencing is
transparent and higher precedence than casting.

I believe that resources and NULL are also scalars but not castable.

C.
Feb 5 '06 #6
On Sun, 05 Feb 2006 21:54:03 GMT, Colin McKinnon
<co**********************@ntlworld.deletemeunlessU RaBot.com> wrote:
Supported types that are not scalar are:
* the 2 compound types array and object
* the 2 special types NULL and resource.

*bzzt* wrong

PHP references are scalar - but in most instances dereferencing is
transparent and higher precedence than casting.


References don't really factor into this discussion.
I believe that resources and NULL are also scalars but not castable.


Maybe you should test your beliefs...

if (!is_scalar(null)) echo 'I believe you are wrong!';

I was a bit surprised that null was not a scalar -- seems logical that
it would be, but I guess the designers of PHP were trying to lump them
in with objects.

Feb 6 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Paul E Collins | last post: by
3 posts views Thread by Mike Vallely | last post: by
37 posts views Thread by Shri | last post: by
4 posts views Thread by Simon Schaap | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
11 posts views Thread by Shisou | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.