In below switch statement, it should have to print only "T","E","S" characters as string "testing" contains these words. But it is printing all characters including "X" and "Z". I can’t use "break;" because it will break operation after first case. (After matching "T" with word "testing") but I want to continue the operation even after every case to find the next character. Please advice if anything is missing. switch(true) { case stristr('testing','t'): echo "T"."<br>"; case stristr('testing','x'): echo "X"."<br>"; case stristr('testing','e'): echo "E"."<br>"; case stristr('testing','z'): echo "Z"."<br>"; case stristr('testing','s'): echo "S"."<br>"; } Code (markup):
What do you want to do ? You have to check the return value of the function stristr , you didn't tell the case statement what is the expected result : case (stristr('testing','t') = 'esting'): PHP:
if (stristr('testing','t')) { echo "T"."<br>"; } PHP: You need to build 5 if statements using the above scheme
$subject = strtolower('testing'); // lowercase version of subject so we can use strpos below instead of stripos (php5 only) $letters = str_split('txezs'); $found = array(); foreach ($letters as $letter) { if (strpos($subject, $letter) !== false) // strpos is faster than stristr() { $found[] = strtoupper($letter); } } echo 'Found: ' . implode(', ', $found) . '<br />'; PHP:
Not sure why you're bothering with str_split()'ing a hardcoded string. I'd directly create an array with the letters.