= 0;$i--){$r.=substr("abcdefg",$i,1);}"."/> = 0;$i--){$r.=substr("abcdefg",$i,1);}".">

Home >Backend Development >PHP Problem >How to reverse string abcdefg in php

How to reverse string abcdefg in php

青灯夜游
青灯夜游Original
2022-04-14 14:01:391804browse

Reversal method: 1. Use the strrev() function, the syntax is "strrev("abcdefg")"; 2. Use the for statement and substr(), the syntax "for($i=string length- 1;$i>=0;$i--){$r.=substr("abcdefg",$i,1);}".

How to reverse string abcdefg in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php reverse characters String "abcdefg"

Method 1: Use the string reversal function strrev()

PHP provides a large number of methods for processing strings Built-in functions, for example, if you want to reverse a string, you can use the strrev() function.

strrev() function reverses a string and returns the reversed string.

Example: Reverse the string "abcdefg"

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="abcdefg";
echo "字符串反转前:".$str."<br>";
echo "字符串反转后:".strrev($str);
?>

How to reverse string abcdefg in php

##Method 2: Use for statement and substr() to splice

  • Use the for statement to traverse the string in reverse order

  • Use the substr() function to intercept characters one by one from back to front and splice them into a new string

Example: Reverse the string "abcdefg"

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="abcdefg";
echo "字符串反转前:<br>";
var_dump($str);

$result = &#39;&#39;;
$len = strlen($str);//获取字符串长度
for ($i = $len-1; $i>=0; $i--){
	$result.= substr($str,$i,1);
}
echo "字符串反转后:<br>";
var_dump($result);
?>

How to reverse string abcdefg in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to reverse string abcdefg in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn