Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php to reverse a string without using loops or builtin functions

Tags:

php

How would you write a short php function to reverse a string. The function must:

  • have only one argument
  • not use the built-in function 'strrev' or 'array_reverse'
  • not a use a looping construct like 'for', 'foreach' or 'while'.
like image 887
Qureshi Avatar asked Dec 04 '22 09:12

Qureshi


1 Answers

Quickly scanning down, these all look so long!

function rev($str) {
    return $str?rev(substr($str,1)).$str[0]:'';
}

Recursive so obviously doesn't work on strings longer than 100 chars.

like image 110
Leigh Avatar answered May 25 '23 11:05

Leigh