小编Sar*_*rge的帖子

在Javascript While循环重复在控制台上运行时从1到5计数的最后一个数字

在控制台上运行以下代码时:

var counter=0; while(counter<5){ console.log(counter); counter++; }

console o\p:0 1 2 3 4 4

而对于以下代码工作正常,而不重复最后一个值:

for(var i=0; i<5; i++){ console.log(i); }

console o\p:0 1 2 3 4

现在,如果我在上面提到的while循环之后放置上面的循环,输出就完全正常了:

var counter=0; while(counter<5){ console.log(counter); counter++; } for(var i=0; i<5; i++){ console.log(i); }

console o\p:0 1 2 3 4 0 1 2 3 4

然而,如果我在for循环后放置while循环,则找到重复的最后一个数字.

for(var i=0; i<5; i++){ console.log(i); } var counter=0;while(counter<5){ console.log(counter); counter++; }

console o\p:0 1 2 3 4 0 1 2 3 4 4

请求all为while循环的这种意外行为提供原因.谢谢.

javascript for-loop while-loop

7
推荐指数
1
解决办法
494
查看次数

比较php中的两个时间戳

所以场景是,我有一家商店,晚上 1:00 开门,晚上 10:00 关门。对于任何当前时间,我只想检查该时间戳是否位于商店开放时间和关闭时间之间。

是的,这很简单,但我仍然不知道为什么,我觉得很难。下面是我正在尝试的一段史诗般的狗屎。

<?php

$t=time();  //for current time
$o = date("Y-m-d ",$t)."01:00:00"; //store open at this time
$c = date("Y-m-d ",$t)."22:00:00"; //store closes at this time

//Yes, its crazy .. but I love echoing variables 
echo "current time = ".$t;
echo PHP_EOL;
echo "Start = ".strtotime($o);
echo PHP_EOL;
echo "End = ".strtotime($c);
echo PHP_EOL;

// below condition runs well, $t is always greater than $o
if($t>$o){
  echo "Open_c1";
}else{
  echo "Close_c1";
}

//Here's where my variable $t, …
Run Code Online (Sandbox Code Playgroud)

php time conditional if-statement strtotime

5
推荐指数
2
解决办法
9732
查看次数