Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ext/standard/basic_functions.c: add puts function #3918

Closed
wants to merge 3 commits into from

Conversation

8 participants
@cup
Copy link

commented Mar 4, 2019

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");
}

To resolve, implement "puts" function similar to Ruby or C:

ext/standard/basic_functions.c: add puts function
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");
    }

To resolve, implement "puts" function similar to Ruby or C:

- http://pubs.opengroup.org/onlinepubs/9699919799/functions/puts.html
- http://ruby-doc.org/core/IO.html#method-i-puts

@krakjoe krakjoe added the RFC label Mar 4, 2019

@carusogabriel
Copy link
Member

left a comment

Please, add some tests to test this function's behaviour.

@cmb69

This comment has been minimized.

Copy link
Contributor

commented Mar 4, 2019

For reference: discussion on internals@.

@krakjoe

This comment has been minimized.

Copy link
Member

commented Mar 5, 2019

@cup test fails ...

@cmb69

This comment has been minimized.

Copy link
Contributor

commented Jun 12, 2019

@cup, any news? Since this would require an RFC, and PHP 7.4 will reach feature freeze in about six weeks, time to hurry up, if you want to see this in the next minor version.

@petk

This comment has been minimized.

Copy link
Contributor

commented Jun 12, 2019

@cup I have fixed my emoji now. Actually, you have missed something here about my reaction (attempt to enlighten the atmosphere and make a more positive feeling of the derailed discussion on the mailing list where your language used there was low quality also - meaning you have a blame there as well). I was in favour of this suggestion. Now, I'm not sure so much anymore... Thank you.

P.S: I've added also a rocket emoji there first and removed it now also ;)

@cmb69

This comment has been minimized.

Copy link
Contributor

commented Jun 12, 2019

@cup, every controversial change to PHP needs the RFC process , and this PR is apparently controversial. Without RFC this feature has no change to get merged (well, someone might actually merge it, but certainly somebody else will revert the commit), since we need a vote, and therefore the RFC process. Of course, you are not obliged to actually start the RFC process. The choice is up to you. :)

@petk, I think we all should try to assess any feature proposal solely based on its own merits, regardless of whether we like the proposers face. :)

@petk

This comment has been minimized.

Copy link
Contributor

commented Jun 12, 2019

@petk, I think we all should try to assess any feature proposal solely based on its own merits, regardless of whether we like the proposers face. :)

Of course. I agree with that. Reactionless RFC in progress where only value add on matters without any influence of possible supporters or people not in favour. Unless the veto happens by some member of the PHP group.

Considering how many languages out there use something like this already, and that PHP wants to be some sort of better CLI tooling platform in the future also, this feature has a clear 2/3+ majority already (maybe not from the people one might expect on the first glance but from the people one never expected)... Now, if one gives up on the way, then it won't be implemented anyway. World won't break apart in any of these cases here I think.

@cup I can guarantee you that most people that will vote on the RFC will vote only in the best interest of the PHP language as a whole and its progress and not be biased based on couple of awkward discussions and reactions.

Good luck.

@krakjoe

This comment has been minimized.

Copy link
Member

commented Jun 13, 2019

@cmb69

This comment has been minimized.

Copy link
Contributor

commented Jun 13, 2019

It might still be not necessary to subscribe to the internals@ mailing list; I'm not subscribed either. Instead I'm subscribed to the mirrored news group, and when I first mailed to the internals@ list, I got a message back, which I had to confirm. Maybe it no longer works this way.

@krakjoe

This comment has been minimized.

Copy link
Member

commented Jun 13, 2019

@nicoSWD

This comment has been minimized.

Copy link

commented Jun 14, 2019

Should't this be a language construct instead of a function, so it can be used without parenthesis just like echo or print?

It would feel a little weird being able to do:

echo 'hello world';
print 'hello world';

... but not:

puts 'hello world';
@krakjoe

This comment has been minimized.

Copy link
Member

commented Jun 14, 2019

@petk

This comment has been minimized.

Copy link
Contributor

commented Jun 14, 2019

Quick thought, I've forgotten to write on the internals... This

puts('something');

will emit the string something and PHP_EOL which is not what many want. I would expect to have \n on all platforms. Even Windows where \r\n will be written.

Inspiration from Rust: https://doc.rust-lang.org/std/macro.println.html

@cup

This comment has been minimized.

Copy link
Author

commented Jun 14, 2019

@petk, no, thats an opinion.

Python print for example does CR+LF on Windows. My preference would be to keep PHP_EOL in place because its the most portable output. Windows users expect CR+LF output, so I would consider Rust and others that are hardcoding LF to be counterexamples.

Of course the final decision isnt up to me, but Im not changing the code to be hardcoded LF, on principle.

@petk

This comment has been minimized.

Copy link
Contributor

commented Jun 14, 2019

Different output on different platforms is also tricky for wider use cases of such function/language construct... For example, if puts (together with something like heredoc) will be used to create a file using this php script.php > file.txt then different line endings won't be a portable solution actually for the end user. Using the same line ending will be. Actually, using hardcoding we could all eliminate different line endings across platforms on the long run also but well, just a thought actually more or less. Yes, it won't fit every use case out there that's for sure... P.S. I'm not sure I've met people that wan't to have CRLF in their output. In most cases they are probably unaware of this, or simply want to have entire output in the same line endings (either LF or CLRF)... It won't cause issues in most cases, but it's an option for some annoying bugs out of nowhere.

Well outputs are a thing to think through anyway. I also haven't tested fully all these options or made some list to get a bigger picture about this... So, yeah probably ok I guess...

@nikic

This comment has been minimized.

Copy link
Member

commented Jun 14, 2019

Inspiration from Rust: https://doc.rust-lang.org/std/macro.println.html

Speaking of ... I think it would make a lot more sense to call this function println rather than puts. The fact that puts() is like print but with a newline will not be obvious to anyone without a C background. (Or even to someone with a C background for that matter, I haven't ever used this function and wouldn't know that it adds a newline.)

@krakjoe

This comment has been minimized.

Copy link
Member

commented Jun 15, 2019

The implementation should really match the proposal in the RFC: close this pull request and create another one.

@cup cup closed this Jun 15, 2019

@cup cup referenced this pull request Jun 15, 2019

Open

Add println function #4267

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.