不同 PHP 版本之间存在未定义变量问题?
我正在使用 XAMPP 1.7.2 (PHP 5.3) 在 winxp 本地主机上进行开发。 有一个功能效果很好。来自 CodeIgniter 模块。
function get_cal_eventTitle($year, $month){
$this->db->where('eventYear =', $year);
$this->db->where('eventMonth =', $month);
$this->db->select('eventTitle');
$query = $this->db->get('tb_event_calendar');
$result = $query->result();
foreach ($result as $row)
{
$withEmptyTitle = $row->eventTitle;
//echo $withEmptyTitle.'<br>';
$noEmptyTitle = str_replace(" ","%20",$withEmptyTitle);
$withUrlTitle = '<a href='.base_url().'index.php/calendar/singleEvent/'.$year.'/'.$month.'/'.$noEmptyTitle.'/'.'>'.$withEmptyTitle.'</a>';
//echo $withUrlTitle.'<br>';
$row->eventTitle = $withUrlTitle;
}
return $result;
}
当我将代码上传到远程服务器(PHP 5.2.9)时。它显示错误如下:
withEmptyTitle undefined variable
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: withUrlTitle
Filename: models/calendar_model.php
Line Number: 54 // $withEmptyTitle = $row->eventTitle;
但是当我启用该行的注释时 echo $withEmptyTitle.'
。它在远程服务器上运行良好。
';
假设 withEmptyTitle
在此处回显 4 月运行事件。
我不知道为什么?您能给我一些建议来解决这个问题吗?感谢您的投入。
I am using XAMPP 1.7.2 (PHP 5.3) to devlope on winxp localhost.
There is a function works quite well. Comes from CodeIgniter Module.
function get_cal_eventTitle($year, $month){
$this->db->where('eventYear =', $year);
$this->db->where('eventMonth =', $month);
$this->db->select('eventTitle');
$query = $this->db->get('tb_event_calendar');
$result = $query->result();
foreach ($result as $row)
{
$withEmptyTitle = $row->eventTitle;
//echo $withEmptyTitle.'<br>';
$noEmptyTitle = str_replace(" ","%20",$withEmptyTitle);
$withUrlTitle = '<a href='.base_url().'index.php/calendar/singleEvent/'.$year.'/'.$month.'/'.$noEmptyTitle.'/'.'>'.$withEmptyTitle.'</a>';
//echo $withUrlTitle.'<br>';
$row->eventTitle = $withUrlTitle;
}
return $result;
}
When I upload my code to remote server(PHP 5.2.9). It show error as this,
withEmptyTitle undefined variable
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: withUrlTitle
Filename: models/calendar_model.php
Line Number: 54 // $withEmptyTitle = $row->eventTitle;
But When I enable the comment for the line echo $withEmptyTitle.'<br>';
. It works well on remote server.
Assuming withEmptyTitle
echo to Apr running event here.
I don't know why? Could you give me some suggestion to fix this issue? Appreciated for your input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您看到的可能不是错误,而是警告。
PHP 可能会抛出警告,因为您使用的变量尚未初始化。听起来您的本地开发 PHP 安装可能已抑制警告消息,而您的实时服务器已启用它们。 (事实上,最佳实践是反过来!)
在这种情况下,
$withEmptyTitle = $row->eventTitle;
可能没有初始化$withEmptyTitle
变量,如果eventTitle
属性返回为未设置。然后,当您尝试在str_replace()
调用中使用变量时,它会落在下面的行并抛出警告。您可以通过以下方式避免此警告:
ini_set()
将其关闭。$row
确实包含eventTitle
属性(在程序的上下文中,如果缺少该属性,则可能意味着数据错误或数据库表设置不正确?无论如何) ,您可以更改 SQL 查询以使用IFNULL()
或至少确保显式查询该字段。[编辑]
我看到您已编辑了该问题,特别是,我注意到以下内容
:请注意
//
.... 这是否意味着该行已被注释掉?您是否以某种方式在服务器上获得了注释了该行的副本?重新收到警告!What you're seeing is probably not an error, but a warning.
PHP may be throwing the warning because you're using a variable that hasn't been initialised. It sounds likely that your local development PHP installation has warning messages supressed, whereas your live server has them enabled. (In fact, best practice would be to have it the other way round!)
In this case, it's possible that
$withEmptyTitle = $row->eventTitle;
is not initialising the$withEmptyTitle
variable, if theeventTitle
property is returning as unset. It would then fall following line and throw the warning when you try to use the variable in thestr_replace()
call.You could avoid this warning by:
ini_set()
to switch it off during the program.isset($withEmptyTitle)
to check that the variable is set prior to actually using it.$row
does actually contain aneventTitle
property (in the context of your program, if it's missing, it may imply bad data or incorrect database table setup? In any case, you could change the SQL query to useIFNULL()
or at least ensure the field is explicitly queried.[EDIT]
I see you've edited the question. In particular, I note the following:
I note the
//
.... does that imply that the line is commented out?? Have you somehow got a copy on the server which has that line commented? That would certainly explain the fact that you're getting warnings!