您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php中字符串的拼接用法详解

2025/12/14 11:55:55发布14次查看
首先和大家说下,学习任何一门语言都要去官网去看文档,因为官方的文档正确性有保证,并且也最有广泛性。
有两个字符串(string)运算符。
第一个是连接运算符(“.”),它返回其左右参数连接后的字符串。
第二个是连接赋值运算符(“.=”),它将右边参数附加到左边的参数之后。
<?php$a = "hello ";$b = $a . "world!"; // now $b contains "hello world!"$a = "hello ";$a .= "world!"; // now $a contains "hello world!"?>
<?php $var = 3; echo "result:" . $var + 3;?>
运行后发现只输出了一个 ‘3’,为什么呢?
因为第一字符串“result3”被创建,这然后被添加到3得到3,非空非数字字符串被转换为0。
如果要输出"result: 6",则代码如下:
<?php $var = 3; echo "result:" . ($var + 3);?>
下面的例子---如果试图用连接运算符加号,你的结果将是这些数字为字符串的结果。
<?phpecho "thr"."ee"; //prints the string "three"echo "twe" . "lve"; //prints the string "twelve"echo 1 . 2; //prints the string "12"echo 1.2; //prints the number 1.2echo 1+2; //prints the number 3?>
大括号服务好替代串联,和他们更快地输入和代码看起来更干净。
推荐手册:php完全自学手册记得用双引号(“”)而不是单引号(‘’)作为其内容是由php parced,因为在单引号(''),你会得到所提供的变量litaral名称
<?php$a = '12345';// this works:echo "qwe{$a}rty"; // qwe12345rty, using bracesecho "qwe" . $a . "rty"; // qwe12345rty, concatenation used// does not work:echo 'qwe{$a}rty'; // qwe{$a}rty, single quotes are not parsedecho "qwe$arty"; // qwe, because $a became $arty, which is undefined?>
<?php$var = "hello";$world = "world";echo "$var" . '$world'; //outputs hello$worldecho "$var" . "$world"; //outputs helloworldecho "$var" . $world; //outputs helloworld?>
可以看出使用使用(‘’)即把单引号里的内容作为了字符,直接echo出来了。而使用(“”)则保留了变量。
相关文章推荐:           
1.php中字符串的连接运算符是什么
2.php几种字符串连接的效率比较
相关视频推荐:
1.独孤九贱(4)_php视频教程
以上就是php中字符串的拼接用法详解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product