Reverse Words in a String: Write a PHP function to reverse the words in a given string.

function reverseWords($str) {
    return implode(' ', array_reverse(explode(' ', $str)));
}

// Example usage:
$string = "Hello world";
echo "Reversed words: " . reverseWords($string);

Related Posts

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Artificial Intelligence