InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

PHP

    PHP strings

    • PHP - split string examples
    • PHP - take last n characters of a string
    • PHP check if string contains something

    PHP Array

    • PHP - associative array value in double quoted string
    • PHP - check if a value is in array
    • PHP - convert array to associative array
    • PHP - print array in one line
    • PHP array foreach - code snippets
    • PHP array map example
    • PHP check if key exists in array
    • PHP sort associative array using custom compare function
    • php get array value with default

    PHP Regex

    • PHP Regex

    PHP Cookbook

    • PHP - assign multi lines string to a variable
    • PHP - call a function with arguments in array examples
    • PHP - get calling file name and line number using debug_backtrace
    • PHP - get class name and file name from an object
    • PHP - get function arguments examples
    • PHP - how to catch errors using set_error_handler
    • PHP - how to initialize static variables
    • PHP - how to use function static variable as cache
    • PHP check if a filename is valid
    • PHP elapsed time
    • PHP execute command and capture shell exit status
    • PHP how to get current url
    • PHP print number to two decimal places
    • PHP remove trailing whitespaces and newline
    • Php equal (==) vs identical (===)
    • Running php eval on code with tags

    PHP functions online

    • PHP functions online
     
    • Home
    • > Tutorials
    • > PHP

    PHP check if key exists in array

    By admin on Jan 27, 2016

    Checking if a key exists in an array and possibly has a non empty value are frequently used when writing php code. Accessing value of non existing key can throw php errors in some cases. Here are some examples which can be used in various scenarios without any php error/notice in log.

    array_key_exists example

    array_key_exists() checks for only presence of key irrespective of its value which may be 0 or null.

    <?php
    $a = array("key1"=>"val1", "key2"=>"");
    if (array_key_exists('key2', $a)) {
      echo "key2 EXISTS\n";
    } else {
      echo "key2 DOES_NOT_EXIST\n";
    }
    ?>
    key2 EXISTS
    
    Env: PHP 7.4.33 (Linux)

    isset() check on key value

    isset() checks is similar to array_key_exists but fails if key value is null. This may be useful in some cases to make code compact and more readable. Also using !is_null() is similar but generates PHP warning. So its better to avoid it.

    <?php
    $a = array("key1"=>"val1", "key2"=>null);
    if (isset($a['key1'])) {
      echo "key1 value is set\n";
    } else {
      echo "key1 value is not set\n";
    }
    if (isset($a['key2'])) {
      echo "key2 value is set\n";
    } else {
      echo "key2 value is not set\n";
    }
    ?>
    key1 value is set
    key2 value is not set
    
    Env: PHP 7.4.33 (Linux)

    !isempty() check on key value

    isempty() checks if key value is empty. This is useful in many cases to make code compact and more readable.

    <?php
    $a = array("key1"=>"val1", "key2"=>"");
    if (!empty($a['key1'])) {
      echo "key1 value is not empty\n";
    } else {
      echo "key1 value is empty\n";
    }
    if (!empty($a['key2'])) {
      echo "key2 value is not empty\n";
    } else {
      echo "key2 value is empty\n";
    }
    ?>
    key1 value is not empty
    key2 value is empty
    
    Env: PHP 7.4.33 (Linux)

    Suggested posts:

    1. Jquery – change table cells color based on value
    2. WordPress – how to make a post sticky on home page
    3. PHP – split string examples
    4. Find authentication methods an ssh server supports
    5. egrep pipe example on Apache access log
    6. React – How to fix SyntaxError – Adjacent JSX elements must be wrapped in an enclosing tag
    7. Bash – iterate over array
    8. PHP – how to initialize static variables
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged PHP, PHP arrays, PHP cookbook, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress