String concatenations

Always use a space between the dot and the concatenated parts to improve readability.

<?php
  $string
= 'Foo' . $bar;
 
$string = $bar . 'foo';
 
$string = bar() . 'foo';
 
$string = 'foo' . 'bar';
?>

When you concatenate simple variables, you can use double quotes and add the string inside, otherwise use single quotes.

<?php
  $string
= "Foo $bar";
?>

When using the concatenating assignment operator ('.='), use a space on each side as with the assignment operator:

<?php
$string
.= 'Foo';
$string .= $bar;
$string .= baz();
?>

Coder confusion

Gribnif - June 3, 2008 - 15:29

These three examples:

  $string = 'Foo' . $bar;
  $string = $bar . 'foo';
  $string = bar() . 'foo';

contradict what the coder module currently reports as "correct". In the case of these, it will complain, "string concatenation should be formatted with a space separating the operators (dot .) and non-quote terms":

  $string = 'Foo'. $bar;
  $string = $bar .'foo';
  $string = bar() .'foo';

Which is correct?

Both are correct

owahab - June 5, 2008 - 14:16

As far as I know both are correct and the first one (space on both sides) is the recommended one for D6 and D7. The second one was the standard until D5.

Close

John Morahan - June 9, 2008 - 20:03

The first is for D7, the second is for D6 and earlier.

http://drupal.org/node/245115#comment-809471

 
 

Drupal is a registered trademark of Dries Buytaert.