# print with newline

- Thread: https://externals.io/message/104545
- Original archive: https://news-web.php.net/php.internals/104545
- Started by Steven Penny on 2019-03-02T19:59:09+00:00
- Messages: 4
- RFC: println(string $data=""): int (Withdrawn) — https://externals.io/rfc/println.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #104545 — Steven Penny — 2019-03-02T19:59:09+00:00

URL: https://externals.io/message/104545#104545

with PHP, several methods are available to produce output:

    echo "hello world\n";
    print "hello world\n";
    print_r("hello world\n");
    var_export("hello world\n");

However all these methods have something in common: they do not produce their
own newline. With each method, the user is required to provide a newline with
"\n", `PHP_EOL` or similar. This is bothersome because many other programming
languages offer such a method. For example Python:

    print('hello world')

Perl:

    use feature say;
    say 'hello world';

Ruby:

    puts 'hello world'

Lua:

    print 'hello world'

Even C:

    #include <stdio.h>
    int main() {
       puts("hello world");
    }

Out of the above examples, I would say Perl and Ruby are most similar to PHP, in
that they also have the "print" method:

    $ perl -e 'print 2; print 3;'
    23

    $ ruby -e 'print 2; print 3;'
    23

However even in this case, "print" can be made to produce a newline by default:

    $ perl -e '$\ = "\n"; print 2; print 3;'
    2
    3

    $ ruby -e '$\ = "\n"; print 2; print 3;'
    2
    3

My request would be one of the following:

1. Modify one or more of "print", "print_r", "var_export" such that they produce
   a newline by default

2. Modify one or more of "print", "print_r", "var_export" such that they have an
   argument similar to Python "end" that controls what follows the input, if
   anything:

   https://docs.python.org/library/functions.html#print

3. Add a new method, perhaps "echoln", "println", "say" or similar, that outputs
   a newline by default

4. introduce a new variable, perhaps "$OUTPUT_RECORD_SEPARATOR", "$ORS", "$\" or
   similar, that controls output record separator

I understand that some of these methods are old and unlikely to change, but if I
do nothing then I have only myself to blame.

---

## #104546 — johannes@schlueters.de — 2019-03-02T22:15:49+00:00

URL: https://externals.io/message/104545#104546

> 1. Modify one or more of "print", "print_r", "var_export" such that
> they produce
>    a newline by default

This is a break of backwards compatibility in a bad way. This breaks
people doing specific output.

> 2. Modify one or more of "print", "print_r", "var_export" such that
> they have an
>    argument similar to Python "end" that controls what follows the
> input, if
>    anything:
> 
>    https://docs.python.org/library/functions.html#print

PHP's echo has the option already:

  echo $foo, PHP_EOL;

not much difference i effort to writing

   print $foo, true;

except that the code is explicit.

> 3. Add a new method, perhaps "echoln", "println", "say" or similar,
> that outputs
>    a newline by default

function println($a) {
    echo $a, PHP_EOL;
}

Can easily be done in library.

> 4. introduce a new variable, perhaps "$OUTPUT_RECORD_SEPARATOR",
> "$ORS", "$\" or
>    similar, that controls output record separator


Such magic is hard to debug and easily leads to bugs in user code.

While sometimes having a shortcut at hand is nice, next request will be
to automatically add a "<br>" as we often produce HTML and thn the ex
request and next and this becomes messy and making everything harder
for very little benefit.

johannes

---

## #104548 — Sara Golemon — 2019-03-03T03:36:50+00:00

URL: https://externals.io/message/104545#104548

> with PHP, several methods are available to produce output:
> [[::snip::]]


<?php

function println(string $x): void {
  echo $x, PHP_EOL;
}

I hereby grant a public domain license to the above code and wish you
godspeed bundling it into a composer package to be enjoyed by users of
every active version of PHP.

-Sara

---

## #104560 — Rowan Collins — 2019-03-03T14:52:29+00:00

URL: https://externals.io/message/104545#104560

> 3. Add a new method, perhaps "echoln", "println", "say" or similar, 
> that outputs
>   a newline by default 


Of the suggestions put forward, this is the only one I can see having 
any chance of succeeding.

However, I think the big reason this doesn't already exist is one that's 
been touched on by other responses: PHP started as, and is still 
primarily regarded as, a language for building websites. In that 
context, newline characters are generally considered "insignificant 
whitespace"; the closest equivalent would be appending '<br>' or '<br 
/>' (depending on the dialect of HTML in use), but you're as likely to 
want "<p>$foo</p>", or "<li>$foo</li>", etc - and that's before we get 
into the tricky topic of escaping.

Even in CLI scripts, as soon as you're building anything intended for 
reuse, you're likely to write a function like log_string() which adds 
information like timestamp, category, severity. The use cases for a new 
function / keyword may therefore be rather limited.

Regards,

-- 
Rowan Collins
[IMSoP]
