Php 简明教程

PHP – String Operators

PHP 中有两个用于处理字符串数据类型的运算符:连接运算符 (".") 和连接赋值运算符 (".="). 阅读本章以了解这些运算符如何在 PHP 中工作。

There are two operators in PHP for working with string data types: concatenation operator (".") and the concatenation assignment operator (".="). Read this chapter to learn how these operators work in PHP.

Concatenation Operator in PHP

点运算符 (".") 是 PHP 的连接运算符。它将两个字符串操作数(右手字符串的字符追加到左手字符串)连接起来并返回一个新字符串。

The dot operator (".") is PHP’s concatenation operator. It joins two string operands (characters of right hand string appended to left hand string) and returns a new string.

$third = $first . $second;

Example

以下示例展示了如何在 PHP 中使用连接运算符:

The following example shows how you can use the concatenation operator in PHP −

<?php
   $x="Hello";
   $y=" ";
   $z="PHP";
   $str=$x . $y . $z;
   echo $str;
?>

它将生成以下 output

It will produce the following output

Hello PHP

Concatenation Assignment Operator in PHP

PHP 还有一个“.=”运算符,可以称之为连接赋值运算符。它通过追加右手操作数的字符来更新在其左边的字符串。

PHP also has the ".=" operator which can be termed as the concatenation assignment operator. It updates the string on its left by appending the characters of right hand operand.

$leftstring .= $rightstring;

Example

以下示例使用连接赋值运算符。两个字符串操作数连接,返回字符串左侧的更新内容:

The following example uses the concatenation assignment operator. Two string operands are concatenated returning the updated contents of string on the left side −

<?php
   $x="Hello ";
   $y="PHP";
   $x .= $y;
   echo $x;
?>

它将生成以下 output

It will produce the following output

Hello PHP