开发者

Undefined variable Issue Between Different PHP Version?

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('开发者_JAVA百科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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜