在php脚本中使用array_key_exists()方法时解析错误

时间:2012-10-27 19:04:42

标签: php array-key-exists

是PHP的新手。我打赌我在做一些愚蠢的事情,因此我在调用array_key_exists()函数的行上得到了解析错误。但由于我是php的新手,因为解释器抛出的错误是如此抽象,我无法弄清楚我的错误。请看这段代码并帮助我:

/* Adding elements into array using [] notations (numbered index) */
$hobbies[] = 'Guitar';
$hobbies[] = 'Reading';
$hobbies[] = 'Singing';
$hobbies[] = 'Travelling';
$hobbyPeople = array(   $hobbies[0] => $names[1],
            $hobbies[1] => $names[3],
            $hobbies[2] => $nickname['shivaraj'],
            $hobbies[3] => $nickname['guru']);
if ($_POST['hobbyName'])
{
    $searchHobby = $_POST['hobbyName'];
    if (array_key_exists($searchHobby, $hobbyPeople)
        print "Yes we have people with hobby you are searching for! And that is: $hobbyPeople[$searchHobby]";
    else
        print "Sorry, we don't have guys with hobby you are searching for!";
}
else
{
print <<<_hobbyTest_
<form method=POST action="$_SERVER[PHP_SELF]">
Enter a hobby you are searching for: <input type="text" name="hobbyName">
<input type="submit" value="Search">
</form>
_hobbyTest_;
}

谢谢, 拉吉

1 个答案:

答案 0 :(得分:3)

你忘记了a)角色

if (array_key_exists($searchHobby, $hobbyPeople))