Q:

Write a PHP program to create a new string of length 2 starting at the given index of a given string

0

Write a PHP program to create a new string of length 2 starting at the given index of a given string.

All Answers

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

<?php
function test($s1, $index)
{ 
 
   return $index + 2 <= strlen($s1) ? substr($s1, $index, 2) : substr($s1, 0, 2);

}

echo test("Hello", 1)."\n";
echo test("Python", 2)."\n";
echo test("on", 1)."\n";
Sample Output:
el
th
on

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