A PHP script is always synchronous - you call a function, you wait for it to finish before your script moves on, however you can use PHP to trigger a second PHP script as a new process via the following generalised command:
shell_exec('/usr/bin/php -f /path/to/script.php &> /dev/null &');
Now this doesn't really help your problem I just included it to answer the exact words of your question - you can't have this second PHP script interact with the first in any way without involving AJAX, at which point you may as well just AJAX the entire thing.
To try and answer the spirit of your question I would like to suggest a different approach:
You have your parent page:
<?php
$hostname = '192.168.0.1';
?>
<h1>Ports for server <?php echo $hostname; ?>:</h1>
<iframe src="/path/to/my/post-scanner-script.php?hostname=<?php echo urlencode($hostname); ?>"></iframe>
Then your child page scans each port in sequence, and as it has finished scanning that port it uses flush()
<?php
// Stuff here to interpret $_GET['hostname'];
foreach ($list as $key => $value) {
$status = !!fsockopen($value["ip"], $value["port"]);
echo "<div>{$value['id']}: " . ($status ? 'OPEN' : 'CLOSED') . "</div>";
flush();
}
What flush()
will do is immediatley send all rendered output to the client machine (it will not send the same content twice), so what you will get is a slowly appearing list of each port and its status, appearing as soon as your script knows the status of the port.
This provides some immediate feedback to the end user as well as a less frustrating wait (it's much more bearable when you can actually see progress being made).