Commit 3fee75d
committed
Proposal: Add
What this does
--------------
This function behaves similarly to this userland code
```
function println(string $data = ''): int {
return printf("%s\n", $data);
}
```
Similarly to `printf("%s\n", $data);`.
- `println` is NOT a keyword. (e.g. functions named println can continue to be
declared outside of the global namespace)
- It returns the number of bytes that were successfully written to standard
output. In the unlikely event that there was an error writing,
this and printf return a smaller number.
- This deliberately always prints the unix newline (`\n`)
**instead of PHP_EOL**.
I would find it very unexpected if println were to behave
differently based on the web server was running it,
e.g. if you moved a website's backend from/to a linux server
to/from a windows server, responses generated by `println` would
suddenly be different.
Additionally, https://www.php-fig.org/psr/psr-2/ recommends that all php
source files contain unix line endings.
If those files contain inline html/text snippets mixed with php+println(),
it would be inconsistent to have `\r\n` in the lines printed by
println() and `\n` anywhere else.
This is same choice of line ending as var_dump, debug_zval_dump,
and var_export use for dumping output.
Otherwise, `println("myArray=" . var_export($myArray, true));`
would be a mix of multiple line ending choices.
Many new languages have elected to always use only the unix newlines,
e.g. https://golang.org/pkg/fmt/#Println and
https://doc.rust-lang.org/std/macro.println.html
Overall, editors do a much better job of detecting newline choices and
displaying different newline choices than they did decades ago.
My opinion is that this anything generating files targeting a specific OS's
line endings should continue to use PHP_EOL or continue to base
the newline choice on the OS of the user requesting the output.
This newline choice differs from the implementation PR for a similar proposal
made 2 years ago https://externals.io/message/104545 ,
for which an RFC was never written.
Differently from printf's argument list, echo, and print,
the argument $data is type checked based on the file's `strict_types` setting.
This is consistent with handling of $data in
`fwrite($stream, string $data): int` or the way format strings($format)
of `printf` are checked.
`println((string)$value)` should be used when strict_types=1 but you are uncertain
of the type.
Reasons to add this
-------------------
1. This is useful for self-contained scripts and a useful helper function to
have overall. E.g. phpt tests of php itself print multiple lines for the
`--EXPECT--` section, and var_dump can be overused even for known strings
because `var_dump(some_function())`
is shorter than `echo some_function() . "\n";`
2. Even if codebases add userland helper equivalents that do exactly this,
If you are new to a codebase, or contribute to multiple
codebases, it is inconvenient to use `xyz_println`,
`ABCUtils::println()`, `echo X, "\n"`, etc., and remember if those different
functions actually use the line endings you think they do.
Additionally, the prefixing is much more verbose.
3. In tutorials or language references that teach a developer
how to use php functionality, it is often preferable to use
functions that append a newline when multiple snippets would be evaluated
together to keep examples simple.
`println("Hello $name");` would be useful to have for introducing PHP
to a new developer before `echo "Hello $name\n";`
(requires explaining escaping first)
or `var_dump("Hello $name");` (that debug representation is rarely useful
for `string(11) "Hello world"`)
E.g. `var_dump` is frequently used instead of
`var_export`, `echo`, or `print` in the manual even for printing strings
with no control characters such as
https://www.php.net/manual/en/function.json-encode.php#example-3972
TODO: Write an rfc document, gather existing counterarguments for/against
naming choices and newline choices, gather examples of other languages that put
a println equivalent in the standard library and their choices.println(string $data = ''): int
1 parent 5a0e406 commit 3fee75d
File tree
5 files changed
+101
-1
lines changed- ext/standard
- tests/general_functions
5 files changed
+101
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
958 | 958 | | |
959 | 959 | | |
960 | 960 | | |
| 961 | + | |
961 | 962 | | |
962 | 963 | | |
963 | 964 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
1486 | 1486 | | |
1487 | 1487 | | |
1488 | 1488 | | |
| 1489 | + | |
| 1490 | + | |
| 1491 | + | |
| 1492 | + | |
1489 | 1493 | | |
1490 | 1494 | | |
1491 | 1495 | | |
| |||
2618 | 2622 | | |
2619 | 2623 | | |
2620 | 2624 | | |
| 2625 | + | |
2621 | 2626 | | |
2622 | 2627 | | |
2623 | 2628 | | |
| |||
3258 | 3263 | | |
3259 | 3264 | | |
3260 | 3265 | | |
| 3266 | + | |
3261 | 3267 | | |
3262 | 3268 | | |
3263 | 3269 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
919 | 919 | | |
920 | 920 | | |
921 | 921 | | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
Lines changed: 42 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
Lines changed: 28 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
0 commit comments