var_dump 改变了函数的结果?
我正在编写一个将进行自动预约的脚本,下面的代码片段用于查找可用时间。然而,我遇到了一些我以前从未听说过的事情。
当我在脚本中使用 var_dump 来查看变量时(我在编码时总是这样做),脚本运行正常。但是,当我删除这些行时,脚本将停止按预期工作。因为我无法使用 var_dump 进行调试,因为它使脚本运行。
function getfirstfree($length, $limit) {
$length = new DateInterval('PT' . $length . 'M');
$table = "randevu";
$today = date('Y-m-d', time());
$q = $this->db->get_where('randevu', array('date' => $today));
$events = $q->result_array();
if ($q->num_rows() > 0) {
for ($i=0; $i < sizeof($q); $i += 1) {
$c_ends = new DateTime($events[$i]['ends']);
$n_starts = new DateTime($events[$i+1]['starts']);
$freetime = $c_ends->diff($n_starts);
$length_w_break = $length;
$length_w_break->i = $length_w_break->i + 10;
var_dump($length_w_break);
var_dump($freetime);
if ($freetime > $length_w_break) {
echo "ok";
return $c_ends->add(new DateInterval('PT10M'));
}
else {
return "no";
}
}
}
}
如果没有 var_dump,脚本将返回 no。使用 var_dump,它返回第一个空闲槽。
从数据库中获取的当前值为
1->开始 12:56:09 结束 12:56:09
2->开始 13:33:11 结束 13:33:11
更新 var_dump($freetime);改变输出
I'm on a script which is going to make automatic appointment and the snippet below is for finding an available time. However, I've come across something that i've never heard of before.
When i use var_dump in my script to see the variables(i always do that when coding) the scripts run OK. However, when i remove these lines, script stops working as intended. as i cannot use var_dump for debugging since it makes the script running.
function getfirstfree($length, $limit) {
$length = new DateInterval('PT' . $length . 'M');
$table = "randevu";
$today = date('Y-m-d', time());
$q = $this->db->get_where('randevu', array('date' => $today));
$events = $q->result_array();
if ($q->num_rows() > 0) {
for ($i=0; $i < sizeof($q); $i += 1) {
$c_ends = new DateTime($events[$i]['ends']);
$n_starts = new DateTime($events[$i+1]['starts']);
$freetime = $c_ends->diff($n_starts);
$length_w_break = $length;
$length_w_break->i = $length_w_break->i + 10;
var_dump($length_w_break);
var_dump($freetime);
if ($freetime > $length_w_break) {
echo "ok";
return $c_ends->add(new DateInterval('PT10M'));
}
else {
return "no";
}
}
}
}
without var_dump the scripts returns no. with var_dump, it returns the first free slot.
current values taken from the database is
1-> starts 12:56:09 ends 12:56:09
2-> starts 13:33:11 ends 13:33:11
update var_dump($freetime); changes the output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在当前的 PHP 版本中,DateInverval 对象和 DateTime 对象的计算会导致此错误。避免使用它们并使用旧式的 UNIX 时间方法计算。
In current PHP versions, DateInverval object and calculations with DateTime objects cause this bug. Avoid using them and use old-style, calculation with unix-time methods.