Question

Am a newbie at PHP. I bet I am doing something silly here and hence am getting the parse error on the line where am invoking array_key_exists() function. But since am a novice at php and since the error thrown by interpreter is so abstract, I cannot make out my mistake. Please see this piece of code and help me:

/* 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_;
}

Thanks, Raj

Was it helpful?

Solution

You forgot a ) character

if (array_key_exists($searchHobby, $hobbyPeople))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top