String and variable concatenation in php

北城以北 提交于 2020-01-29 16:58:33

问题


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

$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.


回答1:


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;



回答2:


Use . for concatenations:

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

Refer to the PHP manual.




回答3:


this will help u

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

o/p: Name\branch\text




回答4:


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

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



回答5:


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

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



回答6:


use

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


来源:https://stackoverflow.com/questions/19954901/string-and-variable-concatenation-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!