Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem with the returning value from stripos

Assuming that $arr1 is an array, and $haystack is a string, when I call this method into my class, it doesn’t respond properly.

$arr1 = [];

// This is a function that generate every string combinations from needle ('dio', 'iod', 'odi', etc)
function permute();

class checkAnagram {
    public function anagram(string $needle, string $haystack, &$arr1): bool {
        if ((strlen($needle) > 1024 || strlen($haystack) > 1024) || strlen($needle) > strlen($haystack)) {
            return 0;
        }

        $needle = strtolower($needle);
        $haystack = strtolower($haystack);

        $str = strtolower($needle);
        permute($str, 0, strlen($str), $arr1);

        for ($i = 0; $i < count($arr1); $i++) {
            if (stripos($haystack, $arr1[$i]) !== false) {
                return true;
            }
        }
        return false;
    }
}

$amara = new checkAnagram();
echo $amara->anagram('Dio', 'Aasadio', $arr1);

It ends to echo nothing, false neither.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your arguments are reversed in stripos. $haystack should come first. Echoing true will return 1, false will return nothing, which is why you aren’t seeing anything.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading