Is it just me? Or am I doing something wrong?

<?php $arr = []; $arr['key'] = NULL; var_dump(isset($arr['key'])); //bool(false) var_dump(is_null($arr['key'])); //bool(true) var_dump(is_null($arr['any_key'])); //bool(true) 

How to check the existence of a key?

    3 answers 3

    If you read the documentation, you will see that isset checks not only the presence of a variable, but its compliance with null if it exists - this is the very unintuitability of PHP that everyone is talking about. In other cases, the behavior of PHP orders to return at least something to it by a non-existent key (possibly throwing out a warning), and this value is null.

    In order to determine the presence of a key in an array, you can use the function array_key_exists .

      What exactly confuses you in the findings? These results you get on all versions of PHP , starting with 5.4. And the existence of a key, except for isset (), can be checked using the function array_key_exists () . Meaning, if you mean it - empty ()

      • because isset is a construction, and the array_key_exists function, isset will work faster, if the key is found and defined and not null, it will work true, and if the key is not, then false. And also by the fact that if $ key null suddenly arrives and the function array_key_exists gives Notice about $ key; - $ key ?? $ key ['name'] ?? null - And
      • @And, what have you written all this for and how is this related to the issue of the vehicle? - Deonis
      • what isset to help or replace ?? - besides, the type null indicates non-existence. - And
      • @And, I think it's better to stop at this, because It can take a long time to decrypt your statements. - Deonis

      null is a special value that means either an uninitialized variable, or a remote ( unset ), or one that is explicitly assigned null .

      Check for the existence of a key using isset()

       if(isset($arr['key'])) { ... } 

      In PHP7, the behavior associated with null not changed.

      • then why isset here false? - Oxel Net
      • @OxelNet Because you explicitly set $ arr ['key'] to null. - cheops