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.
In PHP, we instead use the . (period, or decimal point) character to accomplish string concatenation.
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.
The PHP concatenation operator (.) is used to combine two string values to create one string. Concatenation assignment. Example: <?
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.
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;
Use .
for concatenations:
$var2 = "Name\\branch\\".$var;
Refer to the PHP manual.
this will help u
$var1 = 'text';
$var2 = "Name\branch\".$var1;
o/p: Name\branch\text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With