的「未定義指數」通知彈出,當您使用尚未設置直到那時數組索引值:
<?php
$myarray = array('a'=>1, 'b'=>2);
var_dump($myarray['a']); //outputs int(1) as this is defined
var_dump($myarray['c']); //we defined 'a' and 'b' but not 'c'.
?>
第三行會給:「通知:未定義指數:C在第3行C:\ xampp \ htdocs \ test.php「。
這是通常發生在訪問$ _GET或$ _POST數組時。大多數情況下,原因是您拼錯了索引(例如,您輸入了$_POST['tset']
而不是$_POST['test']
),或者因爲您在提交信息的HTML表單中編輯了<input>
元素,然後忘記重新調整PHP代碼。
您可以通過是否存在索引使用isset()
像這樣的測試以確保一切工作正常:
if(isset($_POST['test'])) {
$myvar = $_POST['test'];
//and then whatever else you intended
}
else {
//the index wasn't defined - you made a mistake, or possibly the user deliberately removed an input from the submitting form
$error = "POST-array is missing the index 'test'!";
//and now make sure that things after this which rely on the 'test' value don't run
}
一個非常普遍的線,你將在腳本的很多發現:
$myvar = isset($_POST['test']) ? $_POST['test'] : 'N/A';
這使用if-else結構的特殊PHP速記。這條線幾乎完全一樣:
if(isset($_POST['test'])) {
$myvar = $_POST['test'];
}
else {
$myvar = 'N/A';
}
爲什麼安裝XAMPP? – elboletaire 2012-04-17 17:59:52
你確定它是正確的腳本,它說第40行... – thumbmunkeys 2012-04-17 18:01:30
可能重複[PHP $ _GET和$ _POST未定義問題](http://stackoverflow.com/questions/1359240/php-get-and- post-undefined-problem) – 2012-04-17 18:16:05