B
B
banny_name2015-10-10 16:50:43
PHP
banny_name, 2015-10-10 16:50:43

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

4 answer(s)
V
Vitaly Inchin ☢, 2015-10-10
@banny_name

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]).

I
Ivanq, 2015-10-10
@Ivanq

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

A
Alexander Zubarev, 2015-10-10
@zualex

Everything has its place, here's a Type Comparison Table for reference

O
OnYourLips, 2015-10-10
@OnYourLips

parsed the source code of frameworks. and in many places I saw that the constructions are used:
if($string == '')

This is due to illiteracy, because your code is better writtenif (!$string) {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question