简体   繁体   中英

PHP empty() behaving strangely

I've come across an issue where on my local LAMP setup (running PHP 5.3), a test for an empty string using empty() works as expected, but the same site running on a remote server (PHP 5.1.6) is behaving differently, in that empty() isn't identifying empty strings. See below:

Form submits an empty text field value, php tests that it isn't empty using the following:

if ( ! empty($_POST['field'])
// On the remote server, the above condition never evaluates an empty field as empty. However, if i change this to the following, it works correctly..
if ($_POST['field'] !== '')

Anyone any clues why this might be happening?

An empty string is not the only "empty" value. The value of $_POST['field'] might very well be null , for example, which !== '' . See: http://php.net/manual/en/function.empty.php .

if ( ! empty($_POST['field']) It is in your code or only in the question? It should be:

if (!empty($_POST['field'])) {
    echo 'something';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM