7

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.

6 Answers 6

9

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;
Sign up to request clarification or add additional context in comments.

1 Comment

In the first one it is taking $var as a string but the second one is working properly. Thanx for the help.
3

Use . for concatenations:

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

Refer to the PHP manual.

1 Comment

The backslash is escaping your end quote.
2

this will help u

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

o/p: Name\branch\text

Comments

1

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

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

1 Comment

However, only with double quotes string
0

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

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

1 Comment

The backslash is escaping your end quote.
-2

use

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

7 Comments

The backslash is escaping your end quote.
@Glavić:having error : Parse error: syntax error, unexpected end of file in /tmp/execpad-ff70b37edf4d/source-ff70b37edf4d on line 3
@BenFortune:that is issue in his string not in variable concatication
@BenFortune:he asked for String and variable concatenation in php
@Glavić:Parse error: syntax error, unexpected 'BU' (T_STRING) in /tmp/execpad-f2a917aad30c/source-f2a917aad30c on line 5
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.