Q:

Write a PHP program to create a new string taking the first 3 characters of a given string and return the string with the 3 characters added at both the front and back. If the given string length is less than 3, use whatever characters are there

0

Write a PHP program to create a new string taking the first 3 characters of a given string and return the string with the 3 characters added at both the front and back. If the given string length is less than 3, use whatever characters are there.

All Answers

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

<?php
function test($str) 
{
   if (strlen($str) < 3)
            {
                return $str.$str.$str;
            }
            else
            {
                $front = substr($str, 0, 3);
                return $front.$str.$front;
            }
        }
echo test("Python")."\n";
echo test("JS")."\n";
echo test("Code")."\n";

Sample Output:

PytPythonPyt
JSJSJS
CodCodeCod

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