2017-04-08 59 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