2 Strings
LogMANOriginal edited this page 2018-11-05 13:24:12 +01:00

Whenever possible use single quote strings

PHP supports both single quote strings and double quote strings. For pure text you must use single quote strings for consistency. Double quote strings are only allowed for special characters (i.e. "\n") or inlined variables (i.e. "My name is {$name}");

Example

Bad

echo "Hello World!";

Good

echo 'Hello World!';

Reference: Squiz.Strings.DoubleQuoteUsage

Add spaces around the concatenation operator

The concatenation operator should have one space on both sides in order to improve readability.

Example

Bad

$text = $greeting.' '.$name.'!';

Good (add spaces)

$text = $greeting . ' ' . $name . '!';

You may break long lines into multiple lines using the concatenation operator. That way readability can improve considerable when combining lots of variables.

Example

Bad

$text = $greeting.' '.$name.'!';

Good (split into multiple lines)

$text = $greeting
. ' '
. $name
. '!';

Reference: Squiz.Strings.ConcatenationSpacing

Use a single string instead of concatenating

While concatenation is useful for combining variables with other variables or static text. It should not be used to combine two sets of static text. See also: Maximum line length

Example

Bad

$text = 'This is' . 'a bad idea!';

Good

$text = 'This is a good idea!';

Reference: Generic.Strings.UnnecessaryStringConcat