比较运算符似乎不起作用


Comparison Opeartor seemingly not working

我试图比较两个值,但当我这样做似乎不工作。我知道这些值是什么,所以它应该是真实的。更糟糕的是,如果我取出其中一个变量并放入数字,它就可以工作了。

$data = simplexml_load_file('xml/heroes/hero.xml')
    or die("Error: Cannot create object");
$hme = $data->hes->he->maxen;       
$hce = $data->hes->he->curen;
$hac = $data->hes->he->lastac;
echo $hce . ' should not be greater than ' . $hme;
if($hce > $hme){
echo 'should be working';
}

输出:

773不应大于20

我认为你的变量是这样的

$hce = "773";
$hme = "20";

在比较它们之前,执行interval

if(intval($hme)>intval($hce))

将字符串转换为整数:

$hme = (int)$data->hes->he->maxen;       
$hce = (int)$data->hes->he->curen;
$hac = (int)$data->hes->he->lastac;

我想你把它们当作字符串了。我认为你需要把它们转换成整数。

简单的函数:

int atoi(char *s)
{
int val = 0;
while (*s)
{
    val *= 10;
    val += (*s) - '0';
    s++;
}
return val;
}