未定义的变量是否在不同的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未定义变量
A PHP Error was encountered
Severity: Notice

Message: Undefined variable: withUrlTitle

Filename: models/calendar_model.php

Line Number: 54 // $withEmptyTitle = $row->eventTitle;  
但是,当我启用“ 2”行的注释时。它在远程服务器上运行良好。 假设
withEmptyTitle
在此处回显到Apr运行事件。 我不知道为什么您能给我一些解决此问题的建议吗?感谢您的输入。     
已邀请:
您看到的可能不是错误,而是警告。 PHP可能会发出警告,因为您使用的是尚未初始化的变量。可能您的本地开发PHP安装中的警告消息被抑制,而您的实时服务器已将其启用。 (实际上,最佳做法是反过来使用!) 在这种情况下,如果
eventTitle
属性返回未设置状态,则
$withEmptyTitle = $row->eventTitle;
可能没有初始化
$withEmptyTitle
变量。当您尝试在
str_replace()
调用中使用该变量时,它将落在下一行并发出警告。 您可以通过以下方式避免此警告: 在PHP.ini中关闭警告消息 在程序执行过程中执行
ini_set()
将其关闭。 使用
isset($withEmptyTitle)
检查变量是否在实际使用之前已设置。 确保“ 10”确实包含“ 6”属性(在程序的上下文中,如果缺少该属性,则可能意味着数据错误或数据库表设置不正确?在任何情况下,您都可以将SQL查询更改为使用“ 12”或在至少确保明确查询该字段。 [编辑] 我看到您已经编辑了问题。我特别注意以下几点:
Line Number: 54 // $withEmptyTitle = $row->eventTitle;  
我注意到
//
....这是否意味着该行已被注释掉??您是否以某种方式在服务器上获得了带有该行注释的副本?那肯定可以解释您得到警告的事实!     

要回复问题请先登录注册