Skip to content

Commit

Permalink
Favor , over . in arguments to echo
Browse files Browse the repository at this point in the history
  • Loading branch information
jimwins committed Oct 16, 2024
1 parent 0f6ce84 commit 4d45290
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions docs/cs-for-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,42 +157,47 @@ now, do not use it (at least until the error handling docs are updated).

3. Use single quotes ' when appropriate.

4. For output use `echo`, instead of `print`.
4. For output use `echo`, instead of `print`. Don't use unnecessary
string concatenation (`.`) with `echo`, use `,` instead.

5. Lowercase HTML tags.

6. Variables in strings:

* Strings in strings

This is of course debatable and subject to personal preference. The two
main methods are inline or concatenation:

It is acceptable to use either string interpolation or concatenation
when composing strings, but `,` should be used to separate multiple
expressions for `echo` is preferred to string concatenation.

```php
$output = "bar is $bar";
echo "bar is $bar";
$output = "bar is {$bar}";
echo "bar is {$bar}";
```
vs
```php
echo 'bar is ' . $bar;
$output = 'bar is ' . $bar;
echo 'bar is ', $bar;
```

All of the above methods are acceptable.

* Arrays in strings

As constants aren't looked for in strings, the following is fine but
may confuse newbies so it's not to be used in examples:

```php
$variable = "an $array[key] key";
echo "an $array[key] key";
```

Instead, consider these:

```php
$variable = "an {$array['key']} key";
echo "an {$array['key']} key";
echo 'an ' . $array['key'] . ' key';
echo 'an ', $array['key'], ' key';
```

## How to write...
Expand Down Expand Up @@ -231,7 +236,7 @@ line where the output occurs, or in the description above the line:
echo $var; // 32
```

For longer example printouts, there are a couple methods which are
For longer example printouts, there are, a couple methods which are
acceptable. Medium sized output may be inline with the example
itself through use of `/* comments */`, for example:

Expand Down

0 comments on commit 4d45290

Please sign in to comment.