I am trying do to this exercise:
“Write a PHP program to exchange the first and last characters in a given string and return the new string.”
The thing I’ve tried to do is this:
function stringReverse($str){
$newStr = array($str);
$tab = [];
for($i = 0 ; $i<count($newStr); $i++){
if($newStr[$i] == [0] && $str[$i] == [count($newStr)-1]){
$tab[$i] = count($newStr)-1 && $tab[$i]= $newStr[0];
}
return $tab;
}
}
echo stringReverse("abcd");
but it’s not working – I get this error :
PHP Notice: Array to string conversion in on line 89 Array
I’m expecting this result :
dbca
Goal of exercise : exchange first and last character of a string.
Can anyone help with this?