Nested if/for loop problem?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chrizzler
Forum Newbie
Posts: 2
Joined: Wed Jun 04, 2008 9:28 am

Nested if/for loop problem?

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Nested if/for loop problem?

Post 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.
chrizzler
Forum Newbie
Posts: 2
Joined: Wed Jun 04, 2008 9:28 am

Re: Nested if/for loop problem?

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Nested if/for loop problem?

Post 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.
Post Reply