Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
You could use fgets to read from STDIN. And to print the message, just use the short tag (<?= whatever, which is a shorthand to <?php echo whatever; ?>): <?='Hello, '.fgets(STDIN).'!'...
Answer
#3: Post edited
- You could use [`fgets`](https://www.php.net/manual/en/function.fgets) to read from [`STDIN`](https://www.php.net/manual/en/wrappers.php.php).
- And to print the message, just use the *short tag* (`<?= whatever`, which is a shorthand to `<?php echo whatever; ?>`):
- ```php
- <?='Hello, '.fgets(STDIN).'!';
- ```
- <sup>[Try it online!](https://tio.run/##K8go@P/fxt5W3SM1JydfR0FdLy09taRYIzjExdNPU09dUd36///y/KKcFAA)</sup>
- Of course you could also do:
- ```php
- <?php
- echo 'Hello, '.fgets(STDIN).'!';
- ```
There are also other options, such as `file_get_contents("php://stdin")` or `stream_get_contents(STDIN)`, but as you said you're code-golfing, I guess the first option would be preferable[^1].- [^1]: I'm not a code-golfer, so not sure if that's the shortest possible way.
- You could use [`fgets`](https://www.php.net/manual/en/function.fgets) to read from [`STDIN`](https://www.php.net/manual/en/wrappers.php.php).
- And to print the message, just use the *short tag* (`<?= whatever`, which is a shorthand to `<?php echo whatever; ?>`):
- ```php
- <?='Hello, '.fgets(STDIN).'!';
- ```
- <sup>[Try it online!](https://tio.run/##K8go@P/fxt5W3SM1JydfR0FdLy09taRYIzjExdNPU09dUd36///y/KKcFAA)</sup>
- Of course you could also do:
- ```php
- <?php
- echo 'Hello, '.fgets(STDIN).'!';
- ```
- And there are also other functions you could use, such as `file_get_contents("php://stdin")` or `stream_get_contents(STDIN)`, but as you said you're code-golfing, I guess the first option would be preferable[^1].
- [^1]: I'm not a code-golfer, so not sure if that's the shortest possible way.
#2: Post edited
- You could use [`fgets`](https://www.php.net/manual/en/function.fgets) to read from [`STDIN`](https://www.php.net/manual/en/wrappers.php.php).
- And to print the message, just use the *short tag* (`<?= whatever`, which is a shorthand to `<?php echo whatever; ?>`):
- ```php
- <?='Hello, '.fgets(STDIN).'!';
- ```
- <sup>[Try it online!](https://tio.run/##K8go@P/fxt5W3SM1JydfR0FdLy09taRYIzjExdNPU09dUd36///y/KKcFAA)</sup>
- Of course you could also do:
- ```php
- <?php
- echo 'Hello, '.fgets(STDIN).'!';
- ```
But as you said you're code-golfing, I guess the first option would be preferable.<sup>I'm not a code-golfer, so not sure if that's the shortest possible way.</sup>
- You could use [`fgets`](https://www.php.net/manual/en/function.fgets) to read from [`STDIN`](https://www.php.net/manual/en/wrappers.php.php).
- And to print the message, just use the *short tag* (`<?= whatever`, which is a shorthand to `<?php echo whatever; ?>`):
- ```php
- <?='Hello, '.fgets(STDIN).'!';
- ```
- <sup>[Try it online!](https://tio.run/##K8go@P/fxt5W3SM1JydfR0FdLy09taRYIzjExdNPU09dUd36///y/KKcFAA)</sup>
- Of course you could also do:
- ```php
- <?php
- echo 'Hello, '.fgets(STDIN).'!';
- ```
- There are also other options, such as `file_get_contents("php://stdin")` or `stream_get_contents(STDIN)`, but as you said you're code-golfing, I guess the first option would be preferable[^1].
- [^1]: I'm not a code-golfer, so not sure if that's the shortest possible way.
#1: Initial revision
You could use [`fgets`](https://www.php.net/manual/en/function.fgets) to read from [`STDIN`](https://www.php.net/manual/en/wrappers.php.php). And to print the message, just use the *short tag* (`<?= whatever`, which is a shorthand to `<?php echo whatever; ?>`): ```php <?='Hello, '.fgets(STDIN).'!'; ``` <sup>[Try it online!](https://tio.run/##K8go@P/fxt5W3SM1JydfR0FdLy09taRYIzjExdNPU09dUd36///y/KKcFAA)</sup> Of course you could also do: ```php <?php echo 'Hello, '.fgets(STDIN).'!'; ``` But as you said you're code-golfing, I guess the first option would be preferable. <sup>I'm not a code-golfer, so not sure if that's the shortest possible way.</sup>