Home > Article > Backend Development > What are string operators in php
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.
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:
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:
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:
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 |
/= | $a /= 2 | $a = $a / 2 | Divide the variable on the left side of the operator by the expression on the right and assign the value to the variable on the left | |
.= | $a .= '2' | $a = $a . '2' | Append the characters on the right to the left | |
%= | $a %= 2 | $a = $ a % 2 | Calculate 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 |
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!