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

Your name:

Reply:


Posted by Justin Koivisto on 02/03/06 23:28

Wayne wrote:
> 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.

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 - justin@koivi.com
http://koivi.com

[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

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