Jump to content


Photo

question about string.


  • Please log in to reply
2 replies to this topic

#1 yong

yong
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 25 October 2006 - 02:44 AM

this is my test code

<?php
var_dump('1' >'a');//bool(false)
var_dump('97'>'a');//ASCⅡ a->97//bool(false)
var_dump('97'<'a');//bool(true)
var_dump('98'>'a');//bool(false)


?>



i want to know why?

why i can receive this result?

what is they compare with eachother's standard?

my english is very poor ,i am chinese

if you can't understand hat i said ,please reaply to me?

think you very much

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 25 October 2006 - 03:19 AM

When comparing strings, "lexical comparison" is used.

1st character of string is compared, then 2nd character, then 3rd, ...

If you compare "123" and "abc", php will check "1" and "a", using an ascii table like this: http://www.lookuptables.com/

If you compare "123" and "12c", php will check "1" and "1", then "2" and "2", then "3" and "c".  It looks for the first difference.

BUT, if you compare "123" and "321", php will compare them as numbers.  It only compares strings if it cannot convert them to numbers.  If you always want string comparison, you can use strcmp()

#3 yong

yong
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 25 October 2006 - 03:56 AM

i understand this

think you very much btherl ..:)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users