gpt4 book ai didi

php - if 进入 for 时的奇怪行为

转载 作者:行者123 更新时间:2023-12-02 09:16:17 28 4
gpt4 key购买 nike

我有这个代码:

for($nrt=0; $nrt<$actualline; $nrt++) {
echo $sidesIndexes[$nrt]." - ".$nrt."<br/>";
if($sidesIndexes[$nrt]==$nrt) {
echo "am I in??? ".$sidesIndexes[$nrt]." is different than ".$nrt."<br/>";
}
}

打印:

# - 0
am I in??? # is different than 0
# - 1
# - 2
# - 3
# - 4
# - 5
# - 6
# - 7
# - 8
# - 9
# - 10
# - 11
# - 12
# - 13
# - 14
# - 15
# - 16
# - 17
# - 18
# - 19

我累了吗?如何才能得到am I in??? # is different than 0信息?

最佳答案

进行比较时,the "#" string is converted to a numeric value ,给出 0

那么是的,0 == 0。

您应该使用===来与检查变量类型进行比较:

if($sidesIndexes[$nrt] === $nrt) {

只有当两个变量都是相同类型(如整数、字符串等)时,这才是正确的。

关于php - if 进入 for 时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5705114/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com