2017-04-08 56 views
0

扭转一个字符串,我收到了一条错误 未初始化字符串偏移量:用于循环使用PHP

这是我的代码

$post = "Test"; 
$strtext = strlen($post); 

for($i=strlen($post);$i>=0;$i--) 
{ 
    echo $post[$i]; 
} 

我回顾我的代码,我认为都是正确的,但我不不认为它是完美的,因为它有一个错误。

结果必须是没有错误的tseT。 ,我不允许使用error_reporting(0);

这是我们的任务tbh,只使用1 for循环反转字符串。

谢谢你的回答。

+1

$ I = strlen的($交)-1; – splash58

+0

'echo strlen($ post)'输出的值是多少? – Terminus

+0

字符串长度为 –

回答

2

相反循环它手动使用PHP函数strrev

溶液1:

<?php 

$post = "Test"; 
echo strrev($post); 

输出:

tseT 

解决方案2:

$post = "Test"; 

for($x=strlen($post)-1; $x>=0; $x--) 
{ 
    echo $post[$x]; 
} 

输出:

tseT 
+0

使用该功能是不允许的,我只允许使用for循环 –

1

尝试这种情况:

for($i=strlen($post) - 1; $i>=0; $i--){ 
    echo $post[$i]; 
} 

作为数组索引是从0n-1