PHP如何使用比较运算符比较字符串? [英] How does PHP compare strings with comparison operators?
问题描述
我正在使用比较运算符比较字符串.
I'm comparing strings with comparison operators.
对于以下两个比较及其结果,我需要一些简短的解释.
I needs some short of explanations for the below two comparisons and their result.
if('ai' > 'i')
{
echo 'Yes';
}
else
{
echo 'No';
}
output: No
为什么这些输出是这种方式?
Why do these output this way?
if('ia' > 'i')
{
echo 'Yes';
}
else
{
echo 'No';
}
Output: Yes
再次,为什么?
也许我忘记了一些基础知识,但我确实需要对这些比较示例进行一些说明才能理解此输出.
Maybe I forgot some basics, but I really need some explanation of these comparison examples to understand this output.
推荐答案
PHP将根据字母顺序使用大于和小于比较运算符来比较字母字符串.
PHP will compare alpha strings using the greater than and less than comparison operators based upon alphabetical order.
-
在第一个示例中,
ai
按字母顺序位于i
之前,因此>
(大于)的检验为false
-该顺序中的较早被认为是小于"而不是大于".
In the first example,
ai
comes beforei
in alphabetical order so the test of>
(greater than) isfalse
- earlier in the order is considered 'less than' rather than 'greater than'.
在第二个示例中,ia
在i
字母顺序之后,因此>
(大于)的检验为true
-后来被认为是大于".
In the second example, ia
comes after i
alphabetical order so the test of >
(greater than) is true
- later in the order being considered 'greater than'.
这篇关于PHP如何使用比较运算符比较字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!