用php反转一个字符串

Ste*_*ley 4 php string reverse

我试图找到一种方法来反转一个字符串,我已经看到了替代方案,但我想以这种方式在盒子外面思考并且不使用任何其他人的代码作为替代,下面的代码反转字符串但我继续得到这个错误:

注意:未定义的偏移量:第15行的C:\ wamp\www\test\index.php中的25

25是被去除的字符串的长度.

//error_reporting(NULL);
$string = trim("This is a reversed string");

//find length of string including whitespace
$len =strlen($string);

//slipt sting into an array
$stringExp = str_split($string);

//deincriment string and echo out in reverse
for ($i = $len; $i >=0;$i--)
{
echo $stringExp[$i];
}
Run Code Online (Sandbox Code Playgroud)

提前致谢

Wes*_*rch 8

你太努力了,总是查阅手册和/或搜索引擎来检查是否有本机功能可以在最终"重新发明轮子"之前做你想做的事情:

strrev - 反转一个字符串

http://php.net/manual/en/function.strrev.php

$string = "This is a reversed string";
echo strrev($string);
// Output: gnirts desrever a si sihT
Run Code Online (Sandbox Code Playgroud)