Reply to Re: WTF? String = Integer in PHP? Strlen() in integers WORKS? HELP!

Your name:

Reply:


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.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация