Home > Backend Development > PHP Problem > How to reverse a string in php and add question marks

How to reverse a string in php and add question marks

青灯夜游
Release: 2023-03-16 13:10:02
Original
1511 people have browsed it

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."?"".

How to reverse a string in php and add question marks

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$str=&#39;ABCcba&#39;;
echo "原字符串:".$str."<br>";
$str2=strrev($str);
echo "反转后字符串:".$str2;

?>
Copy after login

How to reverse a string in php and add question marks

Step 2: After the reversed string, add a question mark "?" character using the concatenation operator "."

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

$str3=$str2."?";
echo "反转并加问号字符后:".$str3;
?>
Copy after login

How to reverse a string in php and add question marks

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template