Home > Article > Backend Development > How to reverse a string in php and add question marks
Steps to reverse a string and add a question mark: 1. Use the strrev() function to reverse the string. The syntax "strrev (original string)" will return a reversed string; 2. After the reversed string, use the concatenation operator "." to add a question mark "?" character, the syntax is "reversed string."?"".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php reverse characters String and add the question mark "?" character
In PHP, you can use the strrev() function and the connection operator "." to achieve this.
strrev() function can reverse a string and return the reversed string
The concatenation operator "." will "? " character and the reversed string are concatenated together.
Implementation steps:
Step 1. Use strrev() function to reverse the string
<?php header('content-type:text/html;charset=utf-8'); $str='ABCcba'; echo "原字符串:".$str."<br>"; $str2=strrev($str); echo "反转后字符串:".$str2; ?>
Step 2: After the reversed string, add a question mark "?" character using the concatenation operator "."
<?php header('content-type:text/html;charset=utf-8'); $str='ABCcba'; echo "原字符串:".$str."<br>"; $str2=strrev($str); echo "反转后字符串:".$str2."<br>"; $str3=$str2."?"; echo "反转并加问号字符后:".$str3; ?>
Expand knowledge:
There are two ways to splice strings in PHP:
One The first is to use the string concatenator .
, the syntax "$str1.$str2
"
String concatenator.
can combine two Or concatenate two or more strings into a new string.
The other is to use the assignment operator .=
, the syntax is "$str1.=$str2
". The
.=
operator appends the characters on the right to the left.
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to reverse a string in php and add question marks. For more information, please follow other related articles on the PHP Chinese website!