Search code examples
phpstring-matchingisnumerictypecasting-operator

check occurance of an integer in a string without using any built in php methods


function findIntInStr($str=''){
$str = 'abc123';
for($i=0;!empty($str[$i]);$i++)
    {
        if(is_numeric($str[$i]))
        {   
            return false;
        }
    }
    return true; }

This question was asked by me in an interview. This was my answer, However the interviewer asked me to check the datatype of character without using any built-in function. How can I do that here?


Solution

  • function findIntInStr($str=''){
    $str = 'abce234';
    for($i=0;!empty($str[$i]);$i++)
        {
            if((string)(int)$str[$i] === (string)$str[$i] )
            {   
                return false;
            }
    
        }
    
        return true;}