Answer the question
In order to leave comments, you need to log in
Why is empty() not used to test for an empty string variable?
parsed the source code of frameworks. and in many places I saw that constructions are used: if($string == '')
Why don't they use empty($string) ?
they said to read the documentation, but everything is fine there, '' - is considered an empty variable....
So why?
Answer the question
In order to leave comments, you need to log in
Look at the result of the execution empty("0");
and you will understand everything.
And one more good way to check if a string is empty - strlen(string)
that is, getting the number of characters in it.
UPD2:
Option from Dmitry - checking the timing for the presence of the first character (Beats the first in speed): isset($string[0])
.
In this case, $string == ''
and the empty($string)
same, but empty checks for emptiness not only strings. For good, it should be used $string === ''
with a triple equal
Everything has its place, here's a Type Comparison Table for reference
parsed the source code of frameworks. and in many places I saw that the constructions are used:
if($string == '')
if (!$string) {
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question