Q:

Write a PHP program to exchange the first and last characters in a given string and return the new string

0

Write a PHP program to exchange the first and last characters in a given string and return the new string.

All Answers

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

<?php
function test($str) 
{
   return strlen($str) > 1 ? substr($str, strlen($str) - 1).substr($str, 1, strlen($str) - 2). substr($str, 0, 1) : $str;
   
}

echo test("abcd")."\n";
echo test("a")."\n";
echo test("xy")."\n";

 

Sample Output:
dbca
a
yx

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