If we know uppercase and lowercase, also ucwords / ucfirst. But, How to print toGGLE cASE ?

🙂

    It depends on your exact requirements, but something like this may work:

    function toggleCase($word) {
        $result = '';
        $len = strlen($word);
        for ($i = 0; $i < $len; ++$i) {
            $result .= $i % 2 == 0 ? strtoupper($word[$i]) : strtolower($word[$i]);
        }
        return $result;
    }

      hmm.. so different with my code before:

      $str = "toggle case";
      $str = str_shuffle(ucfirst($str));
      echo $str;
      

      and the result is funny 😃

      Ouke laserlight, now I can print toGGLE cASE string.

      <?php
      function toggleCase($word) {
         $result = '';
         $len = strlen($word);
         for ($i = 0; $i < $len; ++$i) {
             $result .= $i % 2 == 0 ? strtoupper($word[$i]) : strtolower($word[$i]);
         }
         return $result;
      }
      
      $word = "I love PHPBuilder";
      echo toggleCase($word);
      ?>
      

      Here the result:

      I LoVe pHpBuIlDeR
      

      thanks 🙂

        Write a Reply...