Insert space in a PHP variable

-7

Hello, I see that it's a very simple question, apparently, but I'm finding it very difficult to do this, I probably should be looking wrong on the subject, what I'm trying to do is this:

$var3 = $var1." ".$var2;

In case var3 would get the string var1 + var2 with a space between them, but in that way I can not get what I want, any way I can do that in this style? (in a simple way).

EDIT: I tested the code and now it worked, there probably should have been some runtime error.

    
asked by anonymous 17.01.2018 / 14:17

1 answer

-2

You can use the sprintf function, see:

$var1 = "Olá";
$var2 = "Mundo";
$var3 = sprintf("%s %s", $var1, $var2);
echo $var3; // Olá Mundo

You can use as many parameters as you want in the sprintf function that PHP will put in the sequence that you define:

$texto = sprintf("%s %s %s %s", $var1, $var2, "Outra mensagem", "final");
echo $texto; // Olá Mundo Outra mensagem final

See complete documentation of this feature

    
17.01.2018 / 14:21