简体   繁体   English

PHP while循环忽略其条件

[英]PHP while loop ignores its condition

At the moment i try to create a function in php which simply reads an int value of a field of an array and compares it to a max value. 目前,我尝试在php中创建一个函数,该函数仅读取数组字段的int值并将其与最大值进行比较。
If this max value is reached, it shall set the value of the field to zero, jump to the next field in the array and increase the int value stored inside. 如果达到此最大值,则应将字段的值设置为零,跳到数组中的下一个字段,并增加存储在其中的int值。
If this value also reached the max value do the same as above. 如果该值也达到最大值,请执行与上述相同的操作。
If the above condition isn´t true it shall just increase the stored value in the array. 如果上述条件不成立,则应增加数组中的存储值。

My Code looks like this: 我的代码如下所示:

if($sign_counter[0] === (count($pw_signs) - 1)){
    $counter = 0;
    while($sign_counter[$counter] === (count($pw_signs) - 1)){
        $sign_counter[$counter] = "0";
        $counter++;
    }
    $sign_counter[$counter]++;
}
else{
    $sign_counter[0]++;
}

I allready tested this part of the function several times with different values on my website and browser. 我已经在我的网站和浏览器上用不同的值多次测试了该功能的这一部分。 I also checked if the values were stored correctly in the array and inside needed variables. 我还检查了值是否正确存储在数组中以及所需的变量内。

Thats how my array looks like: 那就是我的数组的样子:

$sign_counter = array("38", "2");

For example: 例如:

$sign_counter array to store int values $ sign_counter数组存储int值
(count($pw_signs) - 1) always equals 38 (because there are 39 fields in the counted array) (count($ pw_signs)-1)始终等于38(因为计数数组中有39个字段)
$counter used to determine the field position inside the array $ counter用于确定字段在数组中的位置

Now if i store the value "38" in the first field and "2" in the second field of the array the code should detect, that the max value is reached in the first field, set the value of the field to 0, then jump to the next field and increase its value by 1. 现在,如果我将值“ 38”存储在数组的第一个字段中,将“ 2”存储在数组的第二个字段中,则代码应检测到,在第一个字段中达到最大值,将该字段的值设置为0,然后跳到下一个字段并将其值增加1。

Instead of what i want to achieve the code just increases the value of the first field of the array. 而不是我想要实现的代码,只是增加了数组的第一个字段的值。
It looks like the while loop just ignores it's own condition but the values itself don't seem to be the problem. 看起来,虽然while循环只是忽略了它自己的条件,但是值本身似乎并不是问题。

I don't really get why the while loop behaves like this. 我真的不明白为什么while循环会像这样。
Do i completly miss something here? 我会完全错过这里吗?

Would appreciate any help. 将不胜感激。
Greetings Sleepy 昏昏欲睡的问候

The problem is that you're storing the values as strings, not numbers, and you're using the === operator, which doesn't perform type coercion between different types. 问题在于您将值存储为字符串而不是数字,并且使用===运算符,该运算符不会在不同类型之间执行类型强制。 count($pw_signs) - 1 will always be a number, not a string, all the === tests will fail. count($pw_signs) - 1始终是数字,而不是字符串,所有===测试都将失败。

Get rid of the quotes around all the numbers and it should work as desired. 删除所有数字周围的引号,它应该可以按需工作。 And if the source of the values is external, convert them to numbers with intval() before storing into the array. 并且,如果值的来源是外部的,则在存储到数组中之前,先使用intval()将其转换为数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM