JasonDavis
JasonDavis

Reputation: 48933

How can I get rid of this PHP notice?

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>";

Upvotes: 1

Views: 272

Answers (7)

Tom Pažourek
Tom Pažourek

Reputation: 10167

Instead of the third line $i = 0;, which is redundant, you could write:

if (!isset($selected_date_month)) $selected_date_month = NULL;

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.

Upvotes: 0

pingw33n
pingw33n

Reputation: 12510

Better validate a variable before using it.

$selected_date_month = (int)@$selected_date_month;

Upvotes: 0

Eddie
Eddie

Reputation: 1958

Try the isset() function:

    $date_combo .= " <option ";
    if (isset($selected_date_month) && $i + 1 == $selected_date_month) {
            $date_combo .= " selected ";
    }
    $date_combo .= " value='" . str_pad($i + 1, 2, "0", STR_PAD_LEFT) . "'>$arr_month[$i]</option>";

Upvotes: 0

Tom Haigh
Tom Haigh

Reputation: 57815

You could check that the variable is set:

if (isset($selected_date_month) && $selected_date_month == $i + 1) 

or suppress the error:

if ($i + 1 == @$selected_date_month)

or turn off notices:

ini_set('error_reporting', E_ALL & ~ E_NOTICE);

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.

Upvotes: 1

mck89
mck89

Reputation: 19231

if (isset($selected_date_month) && $i + 1 == $selected_date_month) {
                $date_combo .= " selected ";
        }

In this way first you check if the $selected_date_month is set and then you compare it with $i+1

Upvotes: 8

KB22
KB22

Reputation: 6969

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.

Upvotes: 0

Bombe
Bombe

Reputation: 83846

  • Check for it being set (using isset).
  • Initialize it with some value.

Upvotes: 0

Related Questions