PHP字符串

EA PHP字符串是一系列字符,即用于存储和处理文本。 在PHP中有4种方法可用于指定字符串。

  1. 单引号
  2. 双引号
  3. heredoc语法
  4. newdoc语法(自PHP 5.3起)

1. 单引号PHP字符串

我们可以通过在单引号中包含文本在PHP中创建一个字符串。 这是在PHP中指定字符串的最简单的方法。如下一个示例 -

<?php  
$str='Hello text within single quote';  
echo $str;  
?>

上面代码执行结果如下 -

Hello text within single quote

我们可以在单个引用的PHP字符串中存储多行文本,特殊字符和转义序列。

<?php  
$str1='Hello text   
multiple line  
text within single quoted string';  
$str2='Using double "quote" directly inside single quoted string';  
$str3='Using escape sequences \n in single quoted string';  
echo "$str1 <br/> $str2 <br/> $str3";  
?>

输出结果如下 -

Hello text multiple line text within single quoted string 
Using double "quote" directly inside single quoted string 
Using escape sequences \n in single quoted string

注意:在单引号PHP字符串中,大多数转义序列和变量不会被解释。 可以使用单引号\'反斜杠和通过\\在单引号引用PHP字符串。

参考下面实例代码 -

<?php  
$num1=10;   
$str1='trying variable $num1';  
$str2='trying backslash n and backslash t inside single quoted string \n \t';  
$str3='Using single quote \'my quote\' and \\backslash';  
echo "$str1 <br/> $str2 <br/> $str3";  
?>

输出结果如下-

trying variable $num1 
trying backslash n and backslash t inside single quoted string \n \t 
Using single quote 'my quote' and \backslash

2. 双引号PHP字符串

在PHP中,我们可以通过在双引号中包含文本来指定字符串。 但转义序列和变量将使用双引号PHP字符串进行解释。

<?php  
$str="Hello text within double quote";  
echo $str;  
?>

上面代码执行输出结果 -

Hello text within double quote
`

现在,不能使用双引号直接在双引号字符串内。

<?php  
$str1="Using double "quote" directly inside double quoted string";  
echo $str1;  
?>

上面代码执行输出结果 -

Parse error: syntax error, unexpected 'quote' (T_STRING) in D:\wamp\www\string1.php on line 2
`

我们可以在双引号的PHP字符串中存储多行文本,特殊字符和转义序列。参考如下代码 -

<?php  
$str1="Hello text   
multiple line  
text within double quoted string";  
$str2="Using double \"quote\" with backslash inside double quoted string";  
$str3="Using escape sequences \n in double quoted string";  
echo "$str1 <br/> $str2 <br/> $str3";  
?>

上面代码执行输出结果 -

Hello text multiple line text within double quoted string 
Using double "quote" with backslash inside double quoted string 
Using escape sequences in double quoted string
`

在双引号字符串中,变量将会被解释,这是因为我们对特殊字符进行了转义。

<?php  
$num1=10;   
echo "Number is: $num1";  
?>

上面代码输出结果为 -

Number is: 10

上一篇: PHP数组函数 下一篇: PHP字符串函数