Avoid
Avoid

Reputation: 353

String and variable concatenation in php

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.

Upvotes: 7

Views: 34195

Answers (6)

d_bhatnagar
d_bhatnagar

Reputation: 1497

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

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

Upvotes: 1

Bhupendra
Bhupendra

Reputation: 258

this will help u

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

o/p: Name\branch\text

Upvotes: 2

Glavić
Glavić

Reputation: 43582

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;

Upvotes: 9

Kevin G Flynn
Kevin G Flynn

Reputation: 231

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

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

Upvotes: 0

KeyNone
KeyNone

Reputation: 9190

Use . for concatenations:

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

Refer to the PHP manual.

Upvotes: 3

vishal shah
vishal shah

Reputation: 222

use

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

Upvotes: -2

Related Questions