Is it just me? (PHP 'EMPTY' function)
Or is the PHP 'EMPTY' function a bit stupid in the fact it deems a variable with the value of either a textual zero or numerical zero to return TRUE to an empty check of that variable?
Surely they know a zero is an actual number. So what if you performed some calculations, i.e. bills - wages, and the result is 0 so you break even in the world.
Its likely a test like this might be used:
<?php
if (empty($variable))
{
echo "There was a problem with the calculation";
}else {
echo "You have ".$variable." money left after paying bills";
}
?>
So basically with the way EMPTY works, you would have to first check if it was zero to do something, then if not check if EMPTY?
That's not really forward progress IMHO.
teammatt3 posted this at 16:26 — 24th February 2008.
He has: 2,102 posts
Joined: Sep 2003
Yeah, I found out about that one the hard way. I don't like that behavior either. I use isset() a lot more because of it.
pr0gr4mm3r posted this at 20:35 — 24th February 2008.
He has: 1,502 posts
Joined: Sep 2006
Well, technically, if your bank account is at 0, it would be empty, right? Makes sense to me.
greg posted this at 07:18 — 25th February 2008.
He has: 1,581 posts
Joined: Nov 2005
I see your point, but you wouldn't use EMPTY to check if a value is other than zero. You use IF ==0 IF<>0 etc for that.
So I disagree still with the way EMPTY is set up.
What about textual zero? It deems that to be empty too. Why? Just because a textual zero "represents" a numerical zero shouldn't make it assume empty.
That's just stupid IMHO
The number zero is a numerical value, and for PHP to determine a variable set to numerical zero to be empty is not logical.
It's not empty, it is 0.
So using EMPTY for checking user input is no good, they might be allowed and correct in entering a numerical or textual zero.
EMPTY should check if a var is empty, i.e. it contains no letters a-Z, numbers 0-9 and no other chars including space.
Then empty would determine correctly that a variable truly is EMPTY and could then be used to a much greater affect with the ISSET function.
ISSET is fine and operates as it should.
If a variable is set to be anything it is set, even blank (not even whitespace) isset will return true.
<?php
$var = ""; //no white space
isset($var) //returns true
?>
I agree with that, it has been set.
The two then would be much more useful. If you want to check if a var is empty regardless of if it was ever set or used, then use EMPTY. If you want to check if a var has ever been used or set, then use ISSET.
Currently you cannot use empty for checking results of calculations or user inputs because the input or calc might have returned zero.
So my question is this, where can EMPTY be used with confidence?
You have to use it where you are checking for user input or form calculations where a single numerical or textual zero is not expected, to be more accurate - zero will never occur.
That makes potential problems.
So even then with user input forms, telling them they did not fill in a field could be incorrect. They might have wanted their username to be a textual zero. And you should be telling them 0 is not a valid username, not they didnt type anything in.
And isset is not ueful for that because 'generally' you have something like $name = $_POST['name'];
even if the user never entered anything, isset will return true.
I know all this is managable and there are ways and means, but IMHO, EMPTY could be much more useful if it returned false for a var with zero.
I'm off to email Rasmus Lerdorf
pr0gr4mm3r posted this at 17:48 — 25th February 2008.
He has: 1,502 posts
Joined: Sep 2006
PHP made the change to call "0" empty in PHP4 - almost like an added feature.
Function information: http://us3.php.net/manual/en/function.empty.php
pr0gr4mm3r posted this at 17:49 — 1st March 2008.
He has: 1,502 posts
Joined: Sep 2006
I just found a handy resource on type comparisons, thanks to StumbleUpon.
PHP type comparison tables
greg posted this at 18:27 — 1st March 2008.
He has: 1,581 posts
Joined: Nov 2005
good link :wall:
Note: Simply doing if ($x) while $x is undefined will generate an error of level E_NOTICE. Instead, consider using empty() or isset() and/or initialize your variables.
I have changed (and improved) some of my code thanks to the above. cheers.
You learn something new every day!
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.