What is the difference between isset(), empty(), and is_null()?
Last updated
All three test variables but answer different questions: isset() asks "does it exist and is it non-null?", empty() asks "is it falsy (or missing)?", and is_null() asks "is it exactly null?".
isset(): Returnstrueif the variable is declared and notnull; safe on undefined variables/array keys without a warning.empty(): Returnstruefor any falsy value:0,"","0",null,false, empty array, and undefined variables.is_null(): Returnstrueonly when the value is exactlynull; warns if the variable was never defined (it's a normal function, not a language construct).Practical gotcha: A variable holding
0isisset()true butempty()true too, so don't useempty()when0is a valid value.
- https://github.com/mingyun/mingyun.github.io/issues/92
- https://www.geeksforgeeks.org/quizzes/php-forms-quiz/
- https://stackoverflow.com/questions/12375833/isset-vs-empty-vs-is-null
- https://github.com/mingyun/mingyun.github.io/issues/121
- https://github.com/335348684/php-engineer-interview-questions
- https://www.interviewbit.com/magento-2-interview-questions/