如何在php中将字符与十六进制值进行比较


How to compare a character with a hex value in php?

我在PHP中遇到以下问题。我想将一个字符(字符串)与十六进制值进行比较,请参阅以下示例代码:

// some key defined in some other application. I only can use KEY1
define("KEY1", 0x31);
// the test value
$sValue = '1';
// the comparison which I would like not to modify
print ($sValue == KEY1) ? "true" : "false";

由于0x31表示49位小数,即字符"1",我希望看到true。相反,我看到了一个false。如何在不使用任何不同于KEY1的东西的情况下实现?也许sValue的类型不正确?

您正在寻找

print ($sValue == chr(KEY1)) ? "true" : "false";

或者,

print (ord($sValue) == KEY1) ? "true" : "false";