Jump to content

[SOLVED] why doesn't this echo 'yes' ? (simple function test, bool)


dsaba

Recommended Posts

<?php
function checkStr($str,$charArr) { //checks if chars of str are within charArr, returns true if it checks, false if it doesn't
    $flag = true;
    $x = -1;
    do {
        $x = $x + 1;
        $char = substr($str,$x,1);
        if (!in_array(strtolower($char),$charArr)) {
            $flag = false;
            echo $char;
        }
    } while($flag || $x < strlen($str));
    return $flag;
}

$str = '133444444444444444444444444444444444';
if (checkStr($str,array('1','2','3','4'))) {
    echo 'yes';
} else {
    echo 'no';
}
?>

 

Why doesn't it echo 'yes', I think it should

-thanks

<?php
function checkStr($str,$charArr) { //checks if chars of str are within charArr, returns true if it checks, false if it doesn't
    $flag = true;
    $x = 0;
    do {
        if (!in_array(strtolower($str{$x}),$charArr)) {
            $flag = false;
            echo $str{$x};
            break;
        }
        $x++;
    } while($x < strlen($str));
    return $flag;
}

$str = '133444444444444444444444444444444444';
if (checkStr($str,array('1','2','3','4'))) {
    echo 'yes';
} else {
    echo 'no';
}
?>

 

a slight;ly more elegant soluntion IMO...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.