[PHP] reverse string without strrev();

2008-02-28 Thread Shelley
Hi all, What do you think is the best way to display string 'abcdef' as 'fedcba'? Any ideas appreciated. Thanks in advance. -- Regards, Shelley -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Aschwin Wesselius
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; -- Aschwin Wesselius social What you would like to be done to you, do that to the

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Aschwin Wesselius
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

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Stut
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.=

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Aschwin Wesselius
Stut wrote: Just because it works doesn't mean it's right. -Stut What I meant was that I tested the script and it worked, so I didn't spot the flaw (wich is obvious and you were right). No big deal. -- Aschwin Wesselius social What you would like to be done to you, do that to the

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Stut
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

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Nathan Rixham
Aschwin Wesselius wrote: Stut wrote: Just because it works doesn't mean it's right. -Stut What I meant was that I tested the script and it worked, so I didn't spot the flaw (wich is obvious and you were right). No big deal. should it not use curlies? $tmp = ''; $str = 'abcdef'; for

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Aschwin Wesselius
Nathan Rixham wrote: should it not use curlies? I don't know. What is the leading preference nowadays? Does it matter? Somewhere in the past I learned it with the square brackets like an array and it still works like that. Maybe it isn't 'right' (Stut?), but that is the way I picked it up.

RE: [PHP] reverse string without strrev();

2008-02-28 Thread Ford, Mike
On 28 February 2008 12:39, Nathan Rixham advised: should it not use curlies? No, they will be deprecated as of PHP6. (Square brackets used to be, but have been undeprecated and are back in favour!) $tmp = ''; $str = 'abcdef'; for ($i = strlen($str)-1; $i = 0; $i--) { $tmp.= $str{$i};

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Nathan Rixham
Ford, Mike wrote: On 28 February 2008 12:39, Nathan Rixham advised: should it not use curlies? No, they will be deprecated as of PHP6. (Square brackets used to be, but have been undeprecated and are back in favour!) $tmp = ''; $str = 'abcdef'; for ($i = strlen($str)-1; $i = 0; $i--) {

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Christoph Boget
should it not use curlies? No, they will be deprecated as of PHP6. (Square brackets used to be, but have been undeprecated and are back in favour!) cheers for the heads up on curlies Mike :) Where can I read more information about this? thnx, Chris -- PHP General Mailing List

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Robert Cummings
On Thu, 2008-02-28 at 12:39 +, Nathan Rixham wrote: Aschwin Wesselius wrote: Stut wrote: Just because it works doesn't mean it's right. -Stut What I meant was that I tested the script and it worked, so I didn't spot the flaw (wich is obvious and you were right). No

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Aschwin Wesselius
Robert Cummings wrote: There's always a tradeoff between speed and memory. Here's the low memory version: ?php $str = '1234567'; str_reverse_in_place( $str ); echo 'Reversed: '.$str.\n; function str_reverse_in_place( $str ) { $a = 0; $z = strlen( $str ) - 1; while( $a $z ) {

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Aschwin Wesselius
Aschwin Wesselius wrote: Sorry, Rob, that function doesn't return anything. ;-) Bugger, it's getting late. Didn't see the . -- Aschwin Wesselius social What you would like to be done to you, do that to the other /social -- PHP General Mailing List (http://www.php.net/) To

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Robert Cummings
On Thu, 2008-02-28 at 16:39 +0100, Aschwin Wesselius wrote: Aschwin Wesselius wrote: Sorry, Rob, that function doesn't return anything. ;-) Bugger, it's getting late. Didn't see the . It wouldn't really be in place if it wasn't a reference. Since the engine would immediately

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Richard Heyes
Sorry, Rob, that function doesn't return anything. ;-) It's not meant to: ?php // ... $str = '123'; str_reverse_in_place($str); echo $str; ? -- Richard Heyes Employ me (!): http://www.phpguru.org/cv -- PHP General Mailing List (http://www.php.net/) To unsubscribe,

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Aschwin Wesselius
Robert Cummings wrote: It wouldn't really be in place if it wasn't a reference. Since the engine would immediately perform a copy on the first write to a passed non reference string :) Still to much to learn. Nice list to learn from thanks. I just started to learn more and more

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Robin Vickery
On 28/02/2008, Robert Cummings [EMAIL PROTECTED] wrote: On Thu, 2008-02-28 at 12:39 +, Nathan Rixham wrote: Aschwin Wesselius wrote: Stut wrote: Just because it works doesn't mean it's right. -Stut What I meant was that I tested the script and it worked,

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Robert Cummings
On Thu, 2008-02-28 at 16:26 +, Robin Vickery wrote: On 28/02/2008, Robert Cummings [EMAIL PROTECTED] wrote: There's always a tradeoff between speed and memory. Here's the low memory version: ?php $str = '1234567'; str_reverse_in_place( $str ); echo 'Reversed:

Re: [PHP] reverse string without strrev();

2008-02-28 Thread Casey
On Thu, Feb 28, 2008 at 3:25 AM, Shelley [EMAIL PROTECTED] wrote: Hi all, What do you think is the best way to display string 'abcdef' as 'fedcba'? Any ideas appreciated. Thanks in advance. -- Regards, Shelley ...What is wrong with strrev()? Am I missing something important