Q:

Write a PHP program to create a new string made of every other character starting with the first from a given string

0

Write a PHP program to create a new string made of every other character starting with the first from a given string.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function test($s)
{
   $result = "";
            for ($i = 0; $i < strlen($s); $i++)
             {
                 if ($i % 2 == 0) 
                 $result = $result.substr($s,$i,1);
             }
            return $result;
 }

echo test("Python")."\n";
echo test("PHP")."\n";
echo test("JS")."\n";
Sample Output:
Pto
PP
J

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now