Nested if/for loop problem?
Posted: Wed Jun 04, 2008 9:39 am
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!:
Does anyone know why?
Thanx!
Chris
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;
}Thanx!
Chris