Reading input (stdin)

Read what you type in the IDE's Input tab with fgets(STDIN) or all at once.

Runs in Zerethon IDE · PHP 8.4 Official PHP documentation ↗

A run cannot ask questions while it runs: type the input in the Input tab before pressing Run.

main.php
<?php

$name = trim(fgets(STDIN));
[$a, $b] = array_map('intval', explode(' ', trim(fgets(STDIN))));
echo "Hello, $name! $a + $b = " . ($a + $b) . "\n";

Input (stdin)

Ada
3 4

Output

Hello, Ada! 3 + 4 = 7
main.php
<?php

$lines = preg_split('/\R/', trim(stream_get_contents(STDIN)));
echo 'total: ', array_sum(array_map('intval', $lines)), "\n";

Input (stdin)

5
10
15

Output

total: 30
All of stdin at once, split into lines.

Watch out: fgets keeps the newline — trim() it. At the end of the input it returns false.