file a.php:
<?php
echo "abcdef";
?>
file b.php:
<?php
$h=popen('php a.php',r);
pclose($h);
?>
question:
I can't see the echo result on console; why and how to see it?
I don't want to do it in file b.php like:echo stream_get_contents($h);
Check the second example in the documentation on popen, it shows exactly how to do that:
This snippet reads from stderr. Remove the pipe to read from stdout.
You cannot see the echo result on the console because it never went to the console. By opening the process in read-mode, its STDOUT was linked the file handle of the open process. The only way the output would get to the console would be if you read from that file handle, then echoed it.
The flow, in other words, is this.
Hope that explains what is going on here. If you want to see the output of a.php on the console, then b.php needs to read it from the stream in $h, then echo it, since only b.php has access to the console.
Alternatively, if you use system() instead of popen(), the output will be output on the calling script's console automatically, because using system() hands over the main script's STDIN and STOUT to the program or script that you call.