Jump to content

question about string.


yong

Recommended Posts

this is my test code
[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)


?>


[/code]

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
Link to comment
Share on other sites

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()
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.