In JavaScript, one can check myObject.key
to see wether the value is there (not undefined) and truthy.
if (myObject.key) ...
Is there a PHP equivalent? Or do I have to keep writing code such as isset(my_array['key']) && my_array['key']
to acheive the desired result? I feel it violates DRY and looks ugly.
There are three "levels" of checking in php:
Checks if a key exists in an array. Returns true even if the value is null
Check if a key exists and is not null - Returns true if the value exists and is not null, but it can be bool(false), int(0), string "" and so on.
Check if a key exists and is truthy, returns false for bool(false), int(0), non-existant keys and null, but true for bool(true), int(1), int(-1), string with length > 0 and so on. Actually the function it is empty(), but often used negated, too. This should be what you are searching for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With