how can i use python's like "For loop" in php to reverse string? -
here python code:
def is_palindrome(s): return revers(s) == s def revers(s): ret = '' ch in s: ret = ch + ret return ret print is_palindrome('racecar') # print true
when convert function php.
function is_palindrome($string){ if (strrev($string) == $string) return true; return false; } $word = "racecar"; var_dump(is_palindrome($word)); // true
both functions works fine but, how can revers string php in loop ??
$string = str_split(hello); $output = ''; foreach($string $c){ $output .= $c; } print $output; // output hello //i did this,
that's work find there way in better way ? $string = "hello"; $lent = strlen($string);
$ret = ''; for($i = $lent; ($i > 0) or ($i == 0); $i--) { $ret .= $string[$i]; #$lent = $lent - 1; } print $output; //output olleh
replace
$output .= $c;
with
$output = $c . $output;
Comments
Post a Comment