Home  >  Article  >  Backend Development  >  How to use array_key_exists and isset in php

How to use array_key_exists and isset in php

墨辰丷
墨辰丷Original
2018-06-12 14:36:171666browse

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 &#39;One&#39; defined? ".array_key_exists("One", $array)."\n");
print("Is &#39;1&#39; defined? ".array_key_exists("1", $array)."\n");
print("Is &#39;Two&#39; defined? ".isset($array["Two"])."\n");
print("Is &#39;2&#39; defined? ".isset($array[2])."\n");
?>

The return result is as follows:

Is &#39;One&#39; defined? 1
Is &#39;1′ defined?
Is &#39;Two&#39; defined? 1
Is &#39;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

Three commonly used functions in the PHP SPL standard library

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn