-1

In PHP, I'm echoing a mix of strings and variables. I want to be able to automatically add a space between each piece, so I don't have to add a ' ' each time.

Example:

echo "This is my" . $string . ".";

This will output "This is mystring". is there some function I can wrap the echo in that will automatically place a space between? Thanks

Egrodo
  • 350
  • 1
  • 5
  • 15

2 Answers2

1

no, you should do it your self

echo "This is my " . $string . "."; 

or easier:

echo "This is my $string."; 

some light reading on php strings: What is the difference between single-quoted and double-quoted strings in PHP?

Community
  • 1
  • 1
  • Sure, I agree, but not an answer to my question. – Egrodo Jul 07 '16 at 00:55
  • @Egrodo so you would write\use a function over simply using: `echo "This is my $string."; ` –  Jul 07 '16 at 00:57
  • No, but this is just a proof-of-concept question. Maybe I would in a situation where I was taking in a lot of non-spaced variables and wanted to dynamically space them. – Egrodo Jul 07 '16 at 01:16
  • 1
    next time, if you used real examples it would help –  Jul 07 '16 at 01:24
1

A user-defined function can automatically add spaces and echo:

$four_five = 'four five';
echospace('one', 'two', 'three', $four_five, 'six');
// one two three four five six

function echospace () {
    echo join(' ', func_get_args());
}
bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
  • im sure it has a user case, for the simple string echoing, using your brain and hitting the space bar is a much better idea –  Jul 07 '16 at 00:48