// Write a function that reverses a string. The input string is given as an array of characters. // Example: Input: s = ["h","e","l","l","o"], Output: ["o","l","l","e","h"]
// with built-in function
function reverseString($s) {
    $s = array_reverse($s);
    return $s;
}
print_r(reverseString(["h", "e" , "l", "l", "o"]));
// with loop
function reverseString($s) {
    $count = count($s);
    $array = [];
    for ($i = 0; $i < $count; $i++) {
	array_unshift($array, $s[$i]); // inserts element at the beginning of the array
    }
    $s = $array;
    return $s;
}
print_r(reverseString(["h", "e" , "l", "l", "o"]));
by Luka Khitaridze
2 years ago
PHP
Array
1
Pro tip: use ```triple backticks around text``` to write in code fences