I want to read the following input in php which is entered in termial
3 5 1 4 0 2 5 2 0 5 1 4 4 1 1 7 2 1 13 2 1 3 3 1 7 2 5 4
Where:
- The first line of input consists of an integer
Twhich is the number of test cases. - The first line of each test case contains an integer n which indicates the size of both arrays.
- The second and third line of each test case contains n space separated integers which are the elements of the first and second arrays respectively.
I am new to PHP, can anybody explain me the code logic of reading the input?
I don’t want to store the input in a file and read it from there.
Any help would be appreciable, Thank you.
Advertisement
Answer
function read_from_console ($prompt = '') {
if ( function_exists('readline') ) {
$line = trim(readline($prompt));
if (!empty($line)) {
readline_add_history($line);
}
} else {
echo $prompt;
$line = trim(fgets(STDIN));
}
return $line;
}
$values = preg_split('/s+/', trim(read_from_console()));