Home >Backend Development >PHP Tutorial >How to use array_key_exists and isset in php
This article mainly introduces the method of PHP to determine whether there is a specified key (key) in the array. It analyzes the usage skills of array_key_exists and isset in PHP with examples. It is of great practical value. Friends who need it can refer to it
The example of this article describes how PHP determines whether the specified key exists in the array. The specific analysis is as follows:
There are two functions in php to determine whether the array contains the specified key, namely array_key_exists and isset
array_key_exists syntax is as follows
array_key_exists($key, $array)
If the key The isset function syntax is as follows:
isset($array[$key])
If the key exists, it returns true
The demo code is as follows:
<?php $array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java"); print("Is 'One' defined? ".array_key_exists("One", $array)."\n"); print("Is '1' defined? ".array_key_exists("1", $array)."\n"); print("Is 'Two' defined? ".isset($array["Two"])."\n"); print("Is '2' defined? ".isset($array[2])."\n"); ?>
The return result is as follows:
Is 'One' defined? 1 Is '1′ defined? Is 'Two' defined? 1 Is '2′ defined?
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
A brief introduction to the decorator pattern in PHP design patterns
A brief introduction to the adapter pattern in PHP design patterns
The above is the detailed content of How to use array_key_exists and isset in php. For more information, please follow other related articles on the PHP Chinese website!