php

How to check if a key exists in an array in PHP

In this tutorial, we are going to see how to check if a key exists in an array in PHP. You can use PHP’s array_key_exists() function to check whether or not a given key or index exists in an array. This function returns TRUE on success or FALSE on failure.
 

How to check if a key exists in an array in PHP
<?php
	$langages = array("PHP"=>"Back-End", "CSS"=>"Front-End", "Python"=>"Back-End");
	 
	// check if 'PHP' key exists in '$langages' array
	if(array_key_exists("PHP", $langages)){
		echo "key exists";
	}else{
		echo "key not exists";
	}
?>

Output:

key exists

 
[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

One thought on “How to check if a key exists in an array in PHP

  • array_key_exists is depricated

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *