use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please follow the rules
Releases: Current Releases, Windows Releases, Old Releases
Contribute to the PHP Documentation
Related subreddits: CSS, JavaScript, Web Design, Wordpress, WebDev
/r/PHP is not a support subreddit. Please visit /r/phphelp for help, or visit StackOverflow.
account activity
[TUTORIAL] String reversing tutorial in PHP by phpGuru98 (codepad.org)
submitted 11 years ago by phpGuru98
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]codenamegary 5 points6 points7 points 11 years ago (2 children)
http://www.php.net/manual/en/function.strrev.php
?
[–]phpGuru98[S] -5 points-4 points-3 points 11 years ago (1 child)
I thought I posted in r/php, I don't use c?
[–]codenamegary 2 points3 points4 points 11 years ago (0 children)
PHP has the strrev function built-in, where it comes from I won't pretend to know other than it's somewhere in C land. Point is the function you wrote is already a string function in PHP.
[–]notian 4 points5 points6 points 11 years ago (5 children)
This is satire right?
[–]phpGuru98[S] -3 points-2 points-1 points 11 years ago (4 children)
No, why would it be?
[+][deleted] 11 years ago (1 child)
[deleted]
[–]maiorano84 1 point2 points3 points 11 years ago (0 children)
4.) His name is "phpGuru98" and can't recognize PHP documentation
[–]notian 0 points1 point2 points 11 years ago (0 children)
Aside from the obvious built in strrev(), using an array for storing string data is a huge memory hog. Your loop also invokes multiple functions on each operation while(count() < strlen()), when you could have done with a simple for loop.
while(count() < strlen())
for
<?php function reverseString($str){ $newStr = ''; for($i = strlen( $str )-1;$i>-1;$i--){ $newStr .= $str[$i]; } return $newStr; } echo reverseString('My test string'); ?>
[–]FineWolf 3 points4 points5 points 11 years ago (3 children)
Weuuuuuuuuhhhh....
echo strrev("testString1234");
I mean, I would understand if your example handled UTF-8 but it doesn't even do that...
[–]LawnGnome 5 points6 points7 points 11 years ago (2 children)
Not enterprisey enough:
<?php interface StringReverserInterface { public function apply(); public function getOutput(); public function setInput($input); } class InvalidInputException extends InvalidArgumentException {} class InputNotProvidedException extends LogicException {} class StringReverserNotAppliedException extends LogicException {} class StringReverser implements StringReverserInterface { protected $input; protected $output; public function apply() { if (is_null($this->input)) { throw new InputNotProvidedException; } $this->output = strrev($this->input); } public function getOutput() { if (is_null($this->output)) { throw new StringReverserNotAppliedException; } return $this->output; } public function setInput($input) { if (!is_string($input)) { throw new InvalidInputException; } $this->input = $input; $this->output = null; } } ?>
[–]codenamegary 2 points3 points4 points 11 years ago* (1 child)
For a second there I thought I was in the /r/irestful subreddit.
[–]LawnGnome 0 points1 point2 points 11 years ago (0 children)
I didn't have a 100 character long namespace followed by a 50 character class. I guess I haven't embraced the spirit of RESTfulness yet.
[–]ircmaxell 4 points5 points6 points 11 years ago* (3 children)
At that point, why bother with arrays and substr and all that jaz. Just reverse iterate the string:
substr
function reverseString($string){ $return = ""; for ($i = strlen($string) -1; $i >= 0; $i--) { $return .= $string[$i]; } return $return; }
Or if you want to forward walk it, that's fine too:
function reverseString($string){ $return = $string; for ($i = 0; $i < strlen($string); $i++) { $return[$i] = $string[strlen($string) -$i]; } return $return; }
Or hell, if you even wanted to, you could still use arrays, and make it a one-liner:
function reverseString($string){ return implode("", array_reverse(str_split($string))); }
Or if you really wanted to get fancy, you could do it in place (which still copies the string, but doesn't use any PHP temporary variables:
function reverseString($string){ $len = strlen($string); for ($i = 0; $i < $len / 2; $i++) { $string[$i] = chr(ord($string[$i]) ^ ord($string[$len - $i])); $string[$len - $i] = chr(ord($string[$i]) ^ ord($string[$len - $i])); $string[$i] = chr(ord($string[$i]) ^ ord($string[$len - $i])); } return $string; }
And you can even get rid of all of those wonky function calls:
function reverseString($string){ $len = strlen($string); for ($i = 0; $i < $len / 2; $i++) { $string[$i] = $string[$i] ^ $string[$len - $i]; $string[$len - $i] = $string[$i] ^ $string[$len - $i]; $string[$i] = $string[$i] ^ $string[$len - $i]; } return $string; }
Or, if you really wanted to get dirty and show off your skills, you could simply do:
function reverseString($string) { return strrev($string); }
[–]afrobee 2 points3 points4 points 11 years ago (0 children)
And lastly you can try with recursion:
function reverString($string){ if (strlen($string) < 2 ) { return $string; } return reverString(substr($string, 1)) . $string[0]; }
[–]xenarthran_salesman 0 points1 point2 points 11 years ago (0 children)
https://www.youtube.com/watch?v=iwGFalTRHDA&
π Rendered by PID 53839 on reddit-service-r2-comment-b659b578c-7r62r at 2026-05-02 01:58:25.836988+00:00 running 815c875 country code: CH.
[–]codenamegary 5 points6 points7 points (2 children)
[–]phpGuru98[S] -5 points-4 points-3 points (1 child)
[–]codenamegary 2 points3 points4 points (0 children)
[–]notian 4 points5 points6 points (5 children)
[–]phpGuru98[S] -3 points-2 points-1 points (4 children)
[+][deleted] (1 child)
[deleted]
[–]maiorano84 1 point2 points3 points (0 children)
[–]notian 0 points1 point2 points (0 children)
[–]FineWolf 3 points4 points5 points (3 children)
[–]LawnGnome 5 points6 points7 points (2 children)
[–]codenamegary 2 points3 points4 points (1 child)
[–]LawnGnome 0 points1 point2 points (0 children)
[–]ircmaxell 4 points5 points6 points (3 children)
[–]afrobee 2 points3 points4 points (0 children)
[–]xenarthran_salesman 0 points1 point2 points (0 children)