Bunu nasıl PHP haber kurtulabilirsiniz?

6 Cevap php

Ben tüm göstermek için raporlama hata var edebilmek olmak istiyorum ve hiçbir fark var çünkü sonsuza kadar sürecekse benim php kod Şablonlar geliştirmeye çalışıyorum.

Line 6 aşağıda kod Bu biraz da,

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

6 Cevap

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

$ Selected_date_month ayarlanmış ise bu şekilde ilk kontrol ve daha sonra $ i 1 ile karşılaştırın

Sen değişken ayarlanmış olduğunu kontrol edebilir:

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

veya hatayı bastırmak:

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

veya bildirimleri kapatın:

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

Hatayı gizlenmesi muhtemelen en iyi seçenek değildir. Seçili tarih olsaydı temiz yaklaşım muhtemelen her durumda değişkeni ayarlamak olacaktır, null için ayarlayın.

Sen isset() var ayarlanmış olup olmadığını kontrol etmek için kullanabilirsiniz. Öyle değil Eğer varsayılan değer çeşit ayarlayabilirsiniz.

Isset () fonksiyonu deneyin:

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

Iyi kullanmadan önce değişkeni doğrulamak.

$selected_date_month = (int)@$selected_date_month;

Bunun yerine third line $i = 0;, hangi redundant, siz yazabilirsiniz edilir:

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

Bu fastest çözüm olacaktır.

Değişkeni set olacağını ve her tekrarında tekrar ayarlanır denetlemek olmazdı.