php php中的字符串和变量连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19954901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:17:53  来源:igfitidea点击:

String and variable concatenation in php

phpconcatenation

提问by Avoid

I am new to PHP and I want to concatenate a string with a variable without any space. I am using a variable $varand a string which is given below.

我是 PHP 的新手,我想将一个字符串与一个没有任何空格的变量连接起来。我正在使用$var下面给出的变量和字符串。

$var   // This is variable
"Name\branch" //This is String

I want to concatenate the string and the variable without any space. I am using code like this:

我想连接字符串和变量而没有任何空格。我正在使用这样的代码:

$var2 = "Name\Branch\ $var" 

But a space is created between them.

但是他们之间创造了一个空间。

回答by Glavi?

Space is there, because you entered it ;)

空间在那里,因为你输入了它;)

Use examples:

使用示例:

$var2 = "Name\Branch\$var";
$var2 = "Name\Branch\" . $var;
$var2 = 'Name\Branch\' . $var;
$var2 = "Name\Branch\{$var}";
$var2 = trim("Name\Branch\ ") . $var;

回答by Basti M

Use .for concatenations:

使用.用于级联:

$var2 = "Name\branch\".$var;

Refer to the PHP manual.

请参阅PHP 手册

回答by Bhupendra

this will help u

这将帮助你

$var1 = 'text';
$var2 = "Name\branch\".$var1;

o/p: Name\branch\text

o/p:名称\分支\文本

回答by d_bhatnagar

You can use { } to use variable's value in a string.

您可以使用 { } 在字符串中使用变量的值。

$result = "Name\Branch\{$var}";

回答by Kevin G Flynn

Please try below code : concatenation $var and $str using dot(.)

请尝试以下代码:使用 dot(.) 连接 $var 和 $str

 $var = 'variable';
 $newvar = "Name\Branch\".$var

回答by vishal shah

use

$var2 = "Name\Branch\".$var;