Re: reverse string without strrev();

From: Date: Thu, 28 Feb 2008 11:53:45 +0000
Subject: Re: reverse string without strrev();
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-270536@lists.php.net to get a copy of this message
On 28 Feb 2008, at 11:40, Aschwin Wesselius wrote:
Stut wrote:
On 28 Feb 2008, at 11:30, Aschwin Wesselius wrote:
Shelley wrote:
Hi all, What do you think is the best way to display string 'abcdef' as 'fedcba'?
$tmp = ''; $str = 'abcdef'; for ($i = strlen($str); $i >= 0; $i--) { $tmp.= $str[$i]; } echo $tmp;
Close, but no cigar. $i should be initialised to strlen($str)-1 otherwise you start one character beyond the end of the string.
Ah, well. You're right, but it works none the less.
Just because it works doesn't mean it's right. -Stut -- http://stut.net/

« previous php.general (#270536) next »