在PHP中避免未定义的索引

时间:2012-11-24 19:27:16

标签: php html

我试图避免未定义的索引。我也使用了isset但是我无法获得理想的结果。

<body>

    <FORM NAME ="form1" METHOD ="POST" ACTION = "test.php">

<INPUT NAME = "search" size="74">
<INPUT TYPE = "Submit" Name = "SearchButton" VALUE = "Search">
</FORM> 

<?php  
    $search =  isset($_POST['search']);
    //$search = $_POST['search'];
if (!isset($_GET[$search])) {
    // category isn't set
    echo 'not set';

} 
 else {

    echo 'set';
}
?>   

2 个答案:

答案 0 :(得分:1)

我想你想要

$search =  isset($_POST['search']) ? $_POST['search']  : 'some default';      
$search =  isset($_GET['search']) ? $_GET['search']  : 'some default';

答案 1 :(得分:0)

当你执行$ _GET [$ search]时,这将评估为$ _GET [0]或$ _GET [1],具体取决于参数'search'是否设置为post变量。 我想你需要像

这样的东西
if( isset($_POST['search']) ) {
  //do something with the variable
}