Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: using empty() for empty string?

Tags:

php

I recently discovered this interesting article by Deceze.
But I'm a bit confused by one of its advises:

never use empty or isset for variables that should exist

Using empty() is not good choice to test if $foo = ''; is empty?

like image 818
fralbo Avatar asked Nov 24 '16 11:11

fralbo


People also ask

What is empty string in PHP?

The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0. 0.0.

Is empty string falsey PHP?

Empty strings are falsy but strings with a value are truthy.

Is empty string same as null PHP?

The value null represents the absence of any object, while the empty string is an object of type String with zero characters. If you try to compare the two, they are not the same.

Can we use empty string?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .


2 Answers

What he means is if you want to check if the string is empty then empty won't do that. Empty can mean false, 0, null. Anything 'falsy'.

E.g. these are all true:

<?php

$string = null;
if (empty($string)) {
    echo "This is true";
}

$string = '';
if (empty($string)) {
    echo "This is true";
}

$string = 0;
if (empty($string)) {
    echo "This is true";
}

If you want to check if the string is an empty string you should do this check for '':

<?php 

$string = '';
if (isset($string) && $string === '') {
    echo "This is true";
}

$string = null;
if (isset($string) && $string === '') {
    echo "This is false";
}
like image 55
Antony Thompson Avatar answered Oct 11 '22 04:10

Antony Thompson


You should not use empty (or isset) if you expect $foo to exist. That means, if according to your program logic, $foo should exist at this point:

if ($foo === '')

Then do not do any of these:

if (isset($foo))
if (empty($foo))

These two language constructs suppress error reporting for undefined variables. That's their only job. That means, if you use isset or empty gratuitously, PHP won't tell you about problems in your code. For example:

$foo = $bar;
if (empty($føø)) ...

Hmm, why is this always true, even when $bar contains the expected value? Because you mistyped the variable name and you're checking the value of an undefined variable. Write it like this instead to let PHP help you:

if (!$føø) ...

Notice: undefined variable føø on line ...

The condition itself is the same, == false (!) and empty produce the same outcome for the same values.

How exactly to check for an empty string depends on what values you do or don't accept. Perhaps $foo === '' or strlen($foo) == 0 is the check you're looking for to ensure you have a string with something in it.

like image 32
deceze Avatar answered Oct 11 '22 03:10

deceze