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开发者_如何学Python 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';
}
精彩评论