Home >Backend Development >PHP Problem >What are string operators in php

What are string operators in php

青灯夜游
青灯夜游Original
2022-06-29 13:48:364517browse

There are two string operators in php: 1. The string connection operator ".", the syntax "$string1.$string2", will return the string after concatenating its left and right parameters, that is, Two or more strings are spliced ​​into a new string; 2. The connection assignment operator ".=", the syntax "$string1 .= $string2", can append the right parameter to the left parameter to form a new string.

What are string operators in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

Characters in php String operators

There are two string operators in PHP:

  • The first one is the concatenation operator (" ."), it returns the concatenated string of its left and right parameters.

  • The second is the connection assignment operator (".="), which appends the right parameter to the left parameter.

1. String concatenation operator .

String concatenation operator . can Concatenate two or more strings into a new string.

Here is an example:

<?php
header("Content-type:text/html;charset=utf-8");
$str1="欢迎来到";
$str2="PHP中文网";
$str3=$str1.$str2;
echo "字符串1:".$str1;
echo "<br>字符串2:".$str2;
echo "<br><br>字符串1和字符串2拼接后:".$str3;
?>

Output result:

What are string operators in php

2. Connection assignment operator.=

In PHP, you can use the format of "$string1 .= $string2" to concatenate strings, .= The operator appends the characters on the right to the left.

Let’s take a look at the following example to understand the .= operator.

<?php
header("Content-type:text/html;charset=utf-8");
$str1="欢迎来到";
echo "字符串1:".$str1;

$str2="PHP中文网";
echo "<br>字符串2:".$str2;

$str1.=$str2;
echo "<br>字符串1和字符串2拼接后:".$str1;
?>

Output result:

What are string operators in php

Extended knowledge: assignment operator

The most basic assignment operator is "=" is used to assign values ​​to variables, and other operators can be used in conjunction with the assignment operator "=" to form a combined assignment operator. The assignment operator assigns the value on the right side of the basic assignment operator "=" to the variable on the left.

The assignment operators in PHP are as shown in the following table:

##divide/=$a /= 2$a = $a / 2Divide the variable on the left side of the operator by the expression on the right and assign the value to the variable on the leftSplicing characters.=$a .= '2'$a = $a . '2' Append the characters on the right to the left Get the remainder%=$a %= 2$a = $ a % 2Calculate the variable on the left side of the operator modulo the value of the expression on the right side, and assign the result to the variable on the left side
Operation Symbol Example Expanded form Meaning
Assignment = $a = 2 $ a = 2 Assign the value of the expression on the right to the variable on the left
Add = $a = 2 $a = $a 2 Assign the variable on the left side of the operator plus the value of the expression on the right side to the variable on the left
minus -= $a -= 2 $a = $a - 2 Subtract the value of the expression on the right from the variable on the left of the operator Assign the quantity on the left
multiplied by *= $a *= 2 $a = $a * 2 Multiply the variable on the left side of the operator by the value of the expression on the right and assign it to the variable on the left
Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What are string operators 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