You're trying much too hard, always consult the manual and/or a search engine to check if there are native functions to do what you want before you end up "reinventing the wheel":

strrev — Reverse a string

http://php.net/manual/en/function.strrev.php

$string = "This is a reversed string";
echo strrev($string);
// Output: gnirts desrever a si sihT
Answer from No Results Found on Stack Overflow
🌐
PHP
php.net › manual › en › function.strrev.php
PHP: strrev - Manual
* @return string The reversed string */ function mb_strrev(string $string, string $encoding = null): string { $chars = mb_str_split($string, 1, $encoding ?: mb_internal_encoding()); return implode('', array_reverse($chars)); } ?> It's faster and flexible than tianyiw function (comment #122953)
🌐
W3Schools
w3schools.com › php › func_string_strrev.asp
PHP strrev() Function
zip_close() zip_entry_close() zip_entry_compressedsize() zip_entry_compressionmethod() zip_entry_filesize() zip_entry_name() zip_entry_open() zip_entry_read() zip_open() zip_read() PHP Timezones ... The strrev() function reverses a string.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-reverse-string
PHP Reverse a String - GeeksforGeeks
September 30, 2024 - ... <?php // PHP function to reverse a string using // recursion and substr() function Reverse($str){ // strlen() used to calculate the // length of the string $len = strlen($str); // Base case for recursion if($len == 1){ return $str; } else{ ...
🌐
Inspector
inspector.dev › home › how to reverse a string in php
How to reverse a string in PHP
February 26, 2025 - The strrev() function reverses a string. It takes a string as input and returns a new string with its characters in reverse order. However, it works best with ASCII strings and may not handle UTF-8 or multibyte strings correctly.
🌐
Medium
medium.com › @codewithweb › php-string-reverse-with-inbuilt-functions-or-without-c1f76bf986d5
php string reverse with inbuilt functions or without | by Tarang Panchal | Medium
September 3, 2024 - answer is in background what php does like “substr($string, int, 1)”. in php it allows you to get perticulur bytes in string. Okay lets get back to topic and there are multiple ways you can reverse the string in php.
🌐
CodeSignal
codesignal.com › learn › courses › practicing-string-operations-and-type-conversions-in-php › lessons › string-manipulation-reversing-words-in-php
String Manipulation: Reversing Words in PHP
You're tasked with considering a string filled with words and writing a PHP function that accepts this string. The function should reverse the character order of each word and form a new string consisting of these reversed words.
Find elsewhere
🌐
Edd Mann
eddmann.com › posts › reversing-a-string-in-php
Reversing a String in PHP - Edd Mann
May 10, 2014 - The most imperative approach to reverse a string is by looping over each character with indexes at each end, swapping their contents upon each iteration. PHP’s ability to access individual characters in an array-like manner turns out to be very useful in this case.
🌐
Reintech
reintech.io › blog › understanding-implementing-php-strrev-function
Understanding and Implementing PHP's `strrev()` Function | Reintech media
January 27, 2026 - The strrev() function accepts a single string parameter and returns its reversed counterpart. Unlike many PHP string functions that work with arrays or multiple parameters, strrev() operates exclusively on individual strings:
🌐
3D Bay
clouddevs.com › home › php guides › exploring php’s strrev() function: reversing strings
Exploring PHP's strrev() Function: Reversing Strings
December 11, 2023 - The strrev() function in PHP is a powerful tool designed specifically for reversing strings. It takes a single argument, the input string that you want to reverse, and returns a new string with the characters in reverse order.
🌐
Sitesbay
sitesbay.com › php-program › php-reverse-string-program-in-php
Reverse String Program in PHP - PHP Program
Reverse String Program - Using strrev() function we can reverse any string in php.
Top answer
1 of 4
3

That is quite a "basic" example, and yet there are different approaches possible.

PSR

There is one things that puts me off a bit: spacing. Spacing makes code more readable, thus easier to understand. Your coding style is too compact.

As already said PHP, like other languages has some widely accepted coding standards like PSR. Observance of which (or the lack thereof) is a telltale sign of how experienced the programmer is.

Coding is often teamwork, which means your colleagues must be able to maintain other colleagues' code (especially after they have left the company...) and adherence to standards facilitates maintenance.

I don´t know about the conditions in which the exercise took place, but a good IDE with linter could reformat the code for you. If you are only allowed to use notepad, then you should still try to write code more or less compliant. At least it would be apparent you are used to those coding standards, and you are just outside of your comfort zone, which they would understand.

Multibyte

Not handling multibyte string could be problematic too, as the code will not always perform as expected.

Concatenation

A minor detail maybe, but as far as I know PHP doesn't have the equivalent of a Stringbuilder, so concatenation is implemented somewhat rudimentary. On longer strings, this could result in poor performance and memory usage. But you don't even need to concatenate. PHP has a yield statement like in Python, so you could simply consume the string character by character, in a generator fashion. Just a thought.

Edge cases

There is one test in your code, but unfortunately it is not useful.

What if I pass a NULL variable to your function? I guess it would return an empty string but this is flawed.

And if a number or a float is passed to the function you could decide to cast it to a string, or reject it. Implementation is up to you, but you can make this function more robust either way.

While it may not be possible to think of every scenario within the allocated time, it is still a good idea to add some comments and TODO list, just to show that you have thought about the possible pitfalls, and you are aware of what is missing to have production-ready code.

2 of 4
3

correctness

It's unclear what the specification

reverse a string

really means in this context. To reverse US ASCII like "hello world" is straightforward, and properly handled by the OP code. But the internet is global, and an input like "«déjà vu»" would fare poorly. Is it "a string"? Or would a sensible specification keep calling out that this or that field is "a multibyte string"?

For a text processing solution that can handle "internet text", prefer to call mb_str_split().

special case

    if (!strlen($str)) {
        return '';
    }

It's hard to see why that's even part of the solution. Surely running the for loop through zero iterations should suffice, producing the same result.

linear complexity

You used the .= catenation operator -- good! It results in \$O(n)\$ linear performance. In order to use it, it was necessary to traverse the source string in reverse order.

Definitely do not traverse characters in forward order to build a result using this:

    $reversed = "{$character}{$reversed}";

It is observed to suffer from \$O(n^2)\$ quadratic performance, taking several seconds for a task that should complete within 20 msec. The trouble is we keep copying "the string so far" into a new result string. In contrast .= requests a "big enough" buffer and then keeps extending the result string, without re-reading and re-re-reading partial results. And even with reallocation copying, amortized cost using a constant growth factor will still remain linear.

🌐
EDUCBA
educba.com › home › software development › software development tutorials › php tutorial › reverse string in php
Reverse String in PHP | Learn Reverse String in PHP Using Various Loops
March 24, 2023 - Then the loop starts running by checking the condition “i1>=0” then the original string’s index is called backward likewise loop will print each index value from the back for each iteration until the condition is FALSE. At last, we will get the Reverse of the string using FOR loop. ... <?php $string1 = "PAVANSAKE"; $length1 = strlen($string1); for ($i1=($length1-1) ; $i1 >= 0 ; $i1--) { echo $string1[$i1]; } ?>
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Tutorialspoint
tutorialspoint.com › php › php_function_strrev.htm
PHP - strrev() Function
The PHP strrev() function is used to reverse a given string. This function takes a single parameter as a string and returns a new string with the characters in reverse order. If you pass a string that has already been reversed by this function, it
🌐
W3Schools
w3schools.com › php › php_string_modify.asp
PHP - Modify Strings
... The strtolower() function returns a string in lower case. ... The str_replace() function replaces some characters with some other characters in a string. ... The strrev() function reverses a string.
🌐
Plus2Net
plus2net.com › php_tutorial › php_string_reverse.php
strrev(): PHP String Reverse
Here's how to reverse a string while preserving HTML entities like < and >: $str = "Hello <World>"; $reversed_str = htmlspecialchars(strrev(htmlspecialchars_decode($str))); echo $reversed_str; // Outputs: >dlroW< olleH · This example demonstrates reversing only the words while keeping their order intact: $sentence = "PHP is great"; $reversed_words = implode(' ', array_reverse(explode(' ', $sentence))); echo $reversed_words; // Outputs: great is PHP ←STRING REFERENCE strlen(): Length of the string →
🌐
C# Corner
c-sharpcorner.com › UploadFile › c63ec5 › reverse-string-and-empty-string-in-php
Reverse String and Empty String in PHP
March 2, 2013 - Example The following example shows how to reverse a string using the strrev() function. The strrev() function takes a string argument like "PHP nrael I" and returns the entire string in reverse order like "I learn PHP".
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-reverse-a-string-in-php.php
How to Reverse a String in PHP
PHP Array Functions PHP String Functions PHP File System Functions PHP Date/Time Functions PHP Calendar Functions PHP MySQLi Functions PHP Filters PHP Error Levels ... You can use the PHP strrev() to reverse the text writing direction of a string.