PHP $_REQUEST['id'] 返回错误


php $_REQUEST['id'] returns error

所以我正在使用index.php?id=MYID进行php导航。我使用了一些随机网站来学习代码。它就在这里

使我的程序成功运行,我的意思是它成功地将用户导航到其他页面。但它显示出某种错误。这是

注意:未定义的索引:C:''xampp''htdocs''php_tests''index.php 第 17 行中的 id

我尝试了一切。这是我在这里的第一个问题,所以我可能无法清除我自己,所以我也请我的导师指导我,如果我的问题有什么问题,也回答我的问题。我的代码如下:

<table width="125" cellpadding="0" cellspacing="0" style="display:inline;">
   <tr>
      <td><a href="index.php?id=main" target="_self" name="main">Home</a></td>
   </tr>
   <tr>
      <td><a href="index.php?id=News" target="_self" name="news">News</a></td>
   </tr>
   <tr>
      <td><a href="index.php?id=MoreInfo" target="_self" name="MoreInfo">MoreInfo</a></td>
   </tr>
</table>
<div id="section" style="height:500px; width=:auto; background-color:#CCC">
   <?php
      $id = $_REQUEST['id']; // this will get the id of the link, this will be explained later.
      switch($id) { // make a case with the id
      default: include('main.php'); //When page is loaded this will be the main content, put the content in main.php(you can change the name)
      break; // close this case
      case "News": include('news.php'); // The content in news.php will be called when id=News
      break; // close this case
      case "MoreInfo": include('MoreInfo.php'); // The content in MoreInfo.php will be called when id=Members
      break; // close this case

      } // close switch
      ?>
</div>

您需要检查值是否设置为:

<?php
$id = ''; // define as empty in default
if(isset($_REQUEST['id'])){
    $id = $_REQUEST['id'];
}
?>

另请记住,如果您未将$id设置为默认值为空,那么您将再次收到未定义的变量通知$id


来自 PHP 手册:

isset — 确定变量是否设置且未为 NULL