Page 1 of 1

Nested if/for loop problem?

Posted: Wed Jun 04, 2008 9:39 am
by chrizzler
Normally if you use only one line of code within a FOR loop or within an IF statement, you can skip the {}. But the following code gives different results!:

Code: Select all

// This code gives desired result (checks if username is like S1234567)
 
function is_student($username) {
    
    $username=strtoupper($username);
    $test_string = "0123456789";
    
    if (substr($username,0,1)=='S' && strlen($username)==8) {
        for ($i=1;$i<8;$i++)
            if (strpos($test_string,substr($username,$i,1))===FALSE) return FALSE;
    } else return FALSE;
    
    return TRUE;
}

Code: Select all

// This shortened form should work (only the {} are removed),
// but gives always FALSE (no code complaints from PHP server)
 
function is_student($username) {
    
    $username=strtoupper($username);
    $test_string = "0123456789";
    
    if (substr($username,0,1)=='S' && strlen($username)==8)
        for ($i=1;$i<8;$i++)
            if (strpos($test_string,substr($username,$i,1))===FALSE) return FALSE;
    else return FALSE;
    
    return TRUE;
}
Does anyone know why?

Thanx!
Chris

Re: Nested if/for loop problem?

Posted: Wed Jun 04, 2008 4:27 pm
by califdon
I'm not sure I can explain why, but the important thing is to simply use brackets except perhaps for the simplest possible conditional statements. There's nothing to be gained by omitting them, and it will make reading the code much easier, at the very least. You've established that trying to omit them results in an error, so just don't do that.

Re: Nested if/for loop problem?

Posted: Thu Jun 05, 2008 3:03 am
by chrizzler
I agree that in this case skipping brackets doesn't have a real advantage.

To be more specific in my question/concern:
My concern is that it 'should' work. If there isn't a logic behind why it doesn't, that means that I now cannot trust PHP anymore with skipping brackets... That means I have to check ALL my code for this awkward behavior...

I think I will file a bug report @ PHP.net and see how they react.

Thanx.

Re: Nested if/for loop problem?

Posted: Thu Jun 05, 2008 3:35 am
by Benjamin
That is not good coding practice.

The reason it's evaluating to false is because the 2nd (nested) IF condition is evaluating to true which is triggering the ELSE code to execute. That code was meant to execute if the 1st IF condition was false, but without the brackets PHP just assigned it to the nearest IF statement.

Here's how I would write it:

Code: Select all

 
function is_student($str_username)
{
    $test_string = "0123456789";
 
    if (!preg_match('#^s.{7}$#i', $str_username))
    {
        return false;
    }
    
    for ($i = 1; $i < 8; $i++)
    {
        if (false === strpos($test_string, substr($str_username, $i, 1)))
        {
            return false;
        }
    }
    
    return true;
}
 
var_dump(is_student('s1234567'));
 
Outputs:
bool(true)
Without brackets, the line between what you intended and what actually occurs when the code executes can very quickly disappear.