我怎样才能摆脱这个 PHP 通知?
我正在尝试改进我的整个站点的 php 代码,这将花费很长时间,因为我希望能够有错误报告来显示所有内容并且没有任何通知。
在下面第 6 行的这一小段代码中,
$selected_ date_month 该代码只是较大函数的一部分代码,因此有时会传入 $selected_date_month 并正确设置,但有时则不然。如果不是,我如何防止未设置 $selected_date_month 的通知?
//month dropdown box
$arr_month = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$date_combo .= ' <select name="' . $pre . 'month" class="' .$style. '"><option value="">Month</option>';
$i = 0;
for ($i = 0; $i <= 11; $i++) {
$date_combo .= " <option ";
if ($i + 1 == $selected_date_month) {
$date_combo .= " selected ";
}
$date_combo .= " value='" . str_pad($i + 1, 2, "0", STR_PAD_LEFT) . "'>$arr_month[$i]</option>";
}
$date_combo .= "</select>";
I am trying to improve my php code sitewide which is going to take forever because I want to be able to have error reporting on to show all and have no notices.
In this little bit of code below on line 6,
$selected_ date_ month
This code is just a portion of code from a larger function, so sometimes $selected_ date_ month is passed in and then it is properly set but sometimes it is not. When it is not how would I prevent a NOTICE for $selected_ date_ month not being set?
//month dropdown box
$arr_month = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$date_combo .= ' <select name="' . $pre . 'month" class="' .$style. '"><option value="">Month</option>';
$i = 0;
for ($i = 0; $i <= 11; $i++) {
$date_combo .= " <option ";
if ($i + 1 == $selected_date_month) {
$date_combo .= " selected ";
}
$date_combo .= " value='" . str_pad($i + 1, 2, "0", STR_PAD_LEFT) . "'>$arr_month[$i]</option>";
}
$date_combo .= "</select>";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
通过这种方式,首先检查 $selected_date_month 是否已设置,然后将其与 $i+1 进行比较
In this way first you check if the $selected_date_month is set and then you compare it with $i+1
您可以检查变量是否已设置:
或抑制错误:
或关闭通知:
隐藏错误可能不是最好的选择。最干净的方法可能是在每种情况下都设置变量,如果没有选择日期,您可以将其设置为空。
You could check that the variable is set:
or suppress the error:
or turn off notices:
Hiding the error is probably not the best option. The cleanest approach would probably be to set the variable in every case, you could set it to null if there was no date selected.
您可以使用 isset() 来检查 var 是否已设置。如果不是,您可以将其设置为某种默认值。
You could use isset() to check whether the var is set. If it's not you can set it to some sort of default value.
尝试 isset() 函数:
Try the isset() function:
在使用变量之前最好对其进行验证。
Better validate a variable before using it.
而不是第三行冗余的
$i = 0;
您可以这样写:这将是最快的, 强>解决方案。
该变量将被设置,并且您不必控制它是否在每次迭代中一次又一次地设置。
Instead of the third line
$i = 0;
, which is redundant, you could write:It would be the fastest solution.
The variable would be set and you wouldn't have to control whether it's set again and again in every iteration.