为什么PHP将String转换为整数? [英] Why does PHP convert String to Integer?

查看:130
本文介绍了为什么PHP将String转换为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道PHP决定什么时候从String转换为Integer,以及

I would like to know on what condition PHP decides when to convert from String to Integer and the other way around

示例:

$key = 0;
$test = 'teststring';
if($key == $test) {
    echo "Why, tell me why!";
}
else {
    echo "That's the way love goes...";
}



我知道我应该使用===来获得更好的结果,我很想知道为什么PHP决定将字符串转换为整数而不是反过来。

I know I should use '===' to get better results, but I'm curious to know why PHP decides to convert the string to integer and not the other way around.

推荐答案

当任一操作数是一个数字时。该手册像这样隐藏着:

It converts strings to numbers when either operand is a number. The manual puts it cryptically like this:

http://php.net/manual/en/language.operators.comparison.php


各种类型,根据下表(按顺序)进行比较。

For various types, comparison is done according to the following table (in order).

Operand 1           Operand 2

null or string      string              Convert NULL to "", numerical
                                        or lexical comparison
...

string, resource    string, resource    Translate strings and resources
or number           or number           to numbers, usual math


阅读此表格时,按顺序部分很重要。

Note that the "in order" part is important when reading this table.

这篇关于为什么PHP将String转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆