不同 PHP 版本之间存在未定义变量问题?

发布于 2024-11-01 04:30:10 字数 1458 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-残月青衣踏尘吟 2024-11-08 04:30:10

您看到的可能不是错误,而是警告

PHP 可能会抛出警告,因为您使用的变量尚未初始化。听起来您的本地开发 PHP 安装可能已抑制警告消息,而您的实时服务器已启用它们。 (事实上​​,最佳实践是反过来!)

在这种情况下,$withEmptyTitle = $row->eventTitle; 可能没有初始化 $withEmptyTitle 变量,如果 eventTitle 属性返回为未设置。然后,当您尝试在 str_replace() 调用中使用变量时,它会落在下面的行并抛出警告。

您可以通过以下方式避免此警告:

  • 在 PHP.ini 中关闭警告消息
  • 在程序运行期间执行 ini_set() 将其关闭。
  • 使用 isset($withEmptyTitle) 检查变量在实际使用之前是否已设置。
  • 确保 $row 确实包含 eventTitle 属性(在程序的上下文中,如果缺少该属性,则可能意味着数据错误或数据库表设置不正确?无论如何) ,您可以更改 SQL 查询以使用 IFNULL() 或至少确保显式查询该字段。

[编辑]

我看到您已编辑了该问题,特别是,我注意到以下内容

Line Number: 54 // $withEmptyTitle = $row->eventTitle;  

:请注意 // .... 这是否意味着该行已被注释掉?您是否以某种方式在服务器上获得了注释了该行的副本?重新收到警告!

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 the eventTitle property is returning as unset. It would then fall following line and throw the warning when you try to use the variable in the str_replace() call.

You could avoid this warning by:

  • Switching warning message off in the PHP.ini
  • Doing ini_set() to switch it off during the program.
  • using isset($withEmptyTitle) to check that the variable is set prior to actually using it.
  • Ensuring that $row does actually contain an eventTitle 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 use IFNULL() or at least ensure the field is explicitly queried.

[EDIT]

I see you've edited the question. In particular, I note the following:

Line Number: 54 // $withEmptyTitle = $row->eventTitle;  

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文