Forum Moderators: coopster

Message Too Old, No Replies

String and long KEYS

I'm about to hate PHP!

         

tata668

9:25 pm on Jan 19, 2006 (gmt 0)

10+ Year Member



I use a STRING key in an array but PHP automatically converts it to a long! This is NOT cool.


$key = "123" ;
$is_string = is_string($key) ; // true: ok

$myArray = array($key => "aValue") ;

$keys = array_keys($myArray) ;;
$is_string = is_string($keys[0]) ; // false: not ok!

bennymack

12:18 am on Jan 20, 2006 (gmt 0)

10+ Year Member



Do yourself a favor and join us over in forum13 [webmasterworld.com] :-D

tata668

12:26 am on Jan 20, 2006 (gmt 0)

10+ Year Member



Hehe!

Well... My main project is way too advanced to even think about changing the language used. But for a future project I will definitively think about Perl!

I hate when a language is doing something behind my back! Grrrr...

jatar_k

12:31 am on Jan 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe try your same test with ctype functions [php.net]

coopster

2:08 am on Jan 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




I hate when a language is doing something behind my back! Grrrr...

I've got news for you, whether it is Perl, PHP, or any other language you have to know the idiosyncrasies of the language. And in this case, it is no secret behind your back as the developers have been quite open about it ;)


A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer.

Resource: [php.net...]

On a side note, the best part is the idiosyncracies between the language and the OS -- that's when the real fun begins!

Ah, the joys of programming! Oh well, hang in there fellas!

tata668

2:36 am on Jan 20, 2006 (gmt 0)

10+ Year Member



coopster:

A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer.

And you also have:


$key = "123" ;
$myArray = array($key => "aValue") ;

$isset = isset($myArray[123]) ; // true
$isset = isset($myArray["123"]) ; // true

It's not really fun to code with pitfalls like this around you! I guess everyone should RTFM 2-3 before starting any project...

I would LOVE to see real hashtables and real arrays in PHP one day.