PHP

Reading strings

We can read a single line string using the built-in functions fscanf() where %s operator casts input stream as a string:

1
2
$s = '';
fscanf(STDIN, "%s\n", $s);

or fgets()

1
$s = trim(fgets(STDIN));

Reading an array of integers

When you need to read an array of integers, you have to read the entire string line and then convert it in array using explode() built-in function. For instance, if your input line is 2 10 24 3 5 25, here is the snippet you’ll need:

1
2
$line = trim(fgets(STDIN));
$a = array_map('intval', explode(' ', $line));

array_map() applies intval() function as a callback to each string item retrieved by explode().

Reading $n rows made up of a single integer

Also here you can use fscanf() function, but in this case you’ll need the %d operator to cast input stream as an integer:

1
2
3
4
5
$numbers = [];
for ($i = 0; $i < $n; $i++) {
    fscanf(STDIN, "%d\n", $n);
    $numbers[] = $n;
}

Writing on STDOUT

When you need to write on stdout streaming use:

1
2
3
4
$result = 'my result';
$stdout = fopen('php://stdout', 'w');
fprintf($stdout, $result);
fclose($stdout);
comments powered by Disqus