What’s missing with Switch Statement?

Discussion in 'PHP' started by rahulephp, Aug 19, 2010.

  1. #1
    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):
     
    rahulephp, Aug 19, 2010 IP
  2. Johnta

    Johnta Peon

    Messages:
    101
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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:
     
    Johnta, Aug 20, 2010 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    If you don't use a break, it'll fall through all cases after the first match. Use ifs instead.
     
    nico_swd, Aug 20, 2010 IP
  4. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if (stristr('testing','t')) {
    echo "T"."<br>";
    }
    
    PHP:
    You need to build 5 if statements using the above scheme
     
    ThomasTwen, Aug 20, 2010 IP
  5. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $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:
     
    exam, Aug 20, 2010 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Not sure why you're bothering with str_split()'ing a hardcoded string. I'd directly create an array with the letters.
     
    nico_swd, Aug 20, 2010 IP
  7. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Only for brevity. Either way is fine.
     
    exam, Aug 20, 2010 IP