Testing for the Existence of a Key in an Array : array_key_exists « Data Structure « PHP






Testing for the Existence of a Key in an Array

 
<?php 
$countries = array('Germany' => 'German', 'France' => 'French', 'Spain' => 'Spanish'); 

$country = 'asdf'; 

printf("<p>%s of our visitors are from %s.</p>\n", array_key_exists($country, $countries) ? 'Some' : 'None', $country); 

?>
  
  








Related examples in the same category

1.Checking a
2.Checking for an element with a particular key
3.Validating a drop-down menu with array_key_exists()