Answers for "array key exists or not in php"

PHP
14

php key in array exists

<?php
$search_array = array('first' => null, 'second' => 4);

// returns false
isset($search_array['first']);

// returns true
array_key_exists('first', $search_array);
?>
Posted by: Guest on June-07-2020
1

array key exists in php

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <?php
    if (array_key_exists('btn1', $_POST)) {
        button1();
    }
    if (array_key_exists('btn2', $_POST)) {
        button2();
    }
    function button1()
    {
        echo 'btn1 is clicked boom!';
    }
    function button2()
    {
        echo 'btn2 is clicked wohoo!';
    }
    ?>
    <br><br>

    <form action="" method="post">
        <button name="btn1">click here</button>
        <button name="btn2">click here</button>
    </form>
</body>
</html>
Posted by: Guest on September-10-2021

Code answers related to "array key exists or not in php"

Browse Popular Code Answers by Language