String:$str = "abcdefg";
Method 1 (directly use PHP’s built-in function strrev($str))
print_r(strrev($str));
Use for loop, str_split($str)
1 2 3 4 5 6 7 8 9 10 | $newArrOne = [];
$newStrOne = '';
$newArrOne = str_split ( $str );
$arrCount = count ( $newArrOne );
for ( $i =0; $i < $arrCount ; $i ++) {
$newStrOne .= $newArrOne [ $i ];
}
echo "<pre class=" brush:php;toolbar:false ">" ;
print_r( $newStrOne );
echo "
|
";
Copy after login
Use for loop, strlen($substr)
1 2 3 4 5 6 7 8 | $newStrTwo = '';
$arrCountTwo = strlen ( $str );
for ( $i =1; $i <= $arrCountTwo ; $i ++) {
$newStrTwo .= substr ( $str , - $i , 1);
}
echo "<pre class=" brush:php;toolbar:false ">" ;
print_r( $newStrTwo ). "\n" ;
echo "
|
";
Copy after login
Use for loop method, strlen($substr)
1 2 3 4 5 6 7 8 | $newStrThree = '';
$arrCountThree = strlen ( $str );
for ( $i = $arrCountThree ; $i >=0; $i --) {
@ $newStrThree .= $str [ $i ];
}
echo "<pre class=" brush:php;toolbar:false ">" ;
print_r( $newStrThree ). "\n" ;
echo "
|
";
Copy after login
For more articles related to string flipping in PHP, please pay attention to the PHP Chinese website!