Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 468
Avoid Avatar asked Nov 13 '13 13:11

Avoid


People also ask

What symbol do you use to join concatenate strings and variables PHP?

In PHP, we instead use the . (period, or decimal point) character to accomplish string concatenation.

What is concatenating string in PHP?

There are two string operators. The first is the concatenation operator ('. '), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('. ='), which appends the argument on the right side to the argument on the left side.

Which of the following is used for concatenation in PHP?

The PHP concatenation operator (.) is used to combine two string values to create one string. Concatenation assignment. Example: <?

What is variable concatenation?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect.


3 Answers

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;
like image 148
Glavić Avatar answered Sep 28 '22 06:09

Glavić


Use . for concatenations:

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

Refer to the PHP manual.

like image 40
KeyNone Avatar answered Sep 28 '22 08:09

KeyNone


this will help u

$var1 = 'text';
$var2 = "Name&#92;branch&#92;".$var1;

o/p: Name\branch\text

like image 24
Bhupendra Avatar answered Sep 28 '22 07:09

Bhupendra