tags:

views:

98

answers:

6

I had a problem with a piece of code i just developed and I have narrowed it down to the number 0 not being accepted.

This is the html

<input type="text" size="1" name="hs" maxlength='3'>

If I enter ZERO into that input box it gives an error saying nothing has been entered.

Once I have passed this through, this is the PHP error handling code

$field = "hs"; 
    if((!$hs) || strlen($hs = trim($hs)) == 0)
    {
        $form->setError($field, "* Home score not entered");

    }

    $field = "as"; 
    if((!$as) || strlen($as = trim($as)) == 0)
    {
        $form->setError($field, "* Away score not entered");

    }

I am getting those errors if I enter the score at a 0. Any ideas why this is happening? Is it something any of you have experienced?

THanks

+5  A: 

!0 is true in PHP, so the first if clause is being executed.

mquander
(As an actual solution, try using `empty($hs)` instead of `!$hs`.)
Amber
@Amber: "0" is considered empty by `empty()`.
BoltClock
Never knew this, but I assumed it was something obvious to you guys that I wasnt aware of. Thanks, ill try empty
Luke
Indeed, empty() won't work. Would one use isset() in this situation? New to php, and can't work out how he's just reading the value $hs straight off, instead of $_POST['hs'] or whatever.
Stephen
Oh ok Bolt, any other solutions?
Luke
@Stephen, there is stuff inbetween the code i have showed. I am just at a stage where I have it in the form $hs and $as.
Luke
Well, if $hs comes from $_POST, a check for isset() may do it. I am unsure if un-filled in form boxes default to unitialised or to "". If the latter, you'll have to check for that explictly, but I'd hope it was the former (in which case, as I said, isset()).
Stephen
Stephen, you definitely want to use is_set(), and you appear to be using register_globals, this is considered *REALLY* bad practice.
altCognito
`isset()` or `$hs != ''` to check for empty strings...
ircmaxell
Yeah, `!isset($hs)` should work.
Amber
+4  A: 

PHP interprets both the text string '0' and the number 0 as false. Implicit type conversion is sometimes handy and sometimes annoying. (What you're getting from the form is the string '0').

Try something like

if (strlen($hs = trim($hs)) == 0) {
    $form->setError($field, "* Home score not entered");
} elseif (!is_numeric($hs)) {
    $form->setError($field, "* Score must be a number");
}
TRiG
This is the code I have adopted and it works perfectly, thankyou.
Luke
I still implore you to avoid register_globals! :)
altCognito
A: 

As others have said, the value 0 is interpreted as false. (so the expression !0 is true)

Hey -- you're not using register_globals are you?

What you should be doing is isset($_GET['hs']), and fetch the value of the variable using $_GET['hs'] as well.

Using register_globals is a HUGE security liability.

altCognito
A: 

You could try this if condition instead:

$hs = trim($hs);

// !== does a strict, type-sensitive comparison against the string '0'
if (($hs !== '0' && !$hs) || strlen($hs) == 0)
{
    $form->setError($field, "* Home score not entered");
}
BoltClock
A: 
if(! isset($hs) || ! is_numeric($hs) || $hs < 0) {
    //error
}

I personally don't know why you would want to do a strlen on it, surely an $hs == "" would suffice?

corrodedmonkee
A: 

I would suggest just dropping the !$hs. Your strlen($hs) should be sufficient for what you're trying to test. The only thing I might add is making user $hs isset so you don't get an undefined error.