Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP String reversal without using extra memory

Tags:

php

How can i reverse $str= "hello\n" to "olleh\n" without using any extra memory variable?

I looked up for prebuilt functions but they all use memory, also, i think string is immutable so basically can this be done without any tweak or creating a new string is the only option?

like image 613
Arihant Avatar asked Dec 03 '25 21:12

Arihant


1 Answers

I don't think anything is immutable in PHP. So

$str = strrev($str);

may be of some use.

Fear the micro-optimization as it will take all your time, for no real result ! :)

like image 53
Armage Avatar answered Dec 05 '25 13:12

Armage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!