PHP Operator (2) Detailed explanation of 'String Operator' examples

怪我咯
Release: 2023-03-07 12:12:01
Original
3515 people have browsed it

What are string operators?

Introduction to string operators: The function of string operators is to concatenate two strings to form a new string.

We introduced the arithmetic operators among PHP operators in the previous chapter. I believe you already have some understanding of the "arithmetic operators" among PHP operators. Today, we will give you a detailed introduction to the "String operator" in PHP operators.

In PHP, there are two types of string operators provided for us:

The first one: connection operator, use " ." means that its function is to connect two strings to form a new string.

Second type: assignment connection operator, represented by ".=", which is the combination of "string operator" and "assignment operator" in PHP operators Combination, regarding the assignment connection operator, we will give you a detailed explanation in the following chapters.

So it can also be said that there is only one string operator, which is the first "string concatenation operator", represented by ".".

PS: There is something to note here. In PHP, the "+" sign is only used as an arithmetic operator and cannot be used as a string operator. In the following example, we will compare Explain the difference between the two.

String operator examples

This example is used to compare the difference between "." and "+" signs, when When "." is used, the two strings of variables $a and $b form a new string 1.5a*11, but if the "+" sign is used, PHP will consider this to be an operation. At this time, if there are string types on both sides of the "+" sign, PHP will automatically convert it to an integer type. If there are letters, it will be output as 0. If it is a string starting with a number, the characters will be intercepted. The number at the head of the string is being calculated.

The example code is as follows:

<?

$a="1.5a*1";          //声明一个字符串变量,以数字开头

$b=1;                 //  什么一个整型变量


$ab=$a.$b;            //使用"."运算符将两个变量进行连接

echo $ab."<br/>";

$ba=$a+$b;           ////使用".+"运算符将两个变量进行连接

echo $ba;

?>
Copy after login

The output result is as follows:

PHP Operator (2) Detailed explanation of String Operator examples

It will be fine through the above example You can see the difference between "." and "+", and you can also see the preliminary application of string operators. In the next section, we will introduce in detail the assignment operator in PHP operations.



The above is the detailed content of PHP Operator (2) Detailed explanation of 'String Operator' examples. For more information, please follow other related articles on the PHP Chinese website!

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