How can persistent TCP/IP connections be beneficial when developing software for handling console scripts in PHP?

Persistent TCP/IP connections can be beneficial when developing software for handling console scripts in PHP because they allow for faster communication between the client and server by keeping the connection open for multiple requests. This can improve performance and reduce the overhead of establishing a new connection for each request, especially when dealing with multiple console scripts running concurrently.

<?php

// Establish a persistent TCP/IP connection
$socket = pfsockopen('127.0.0.1', 8080, $errno, $errstr);

if (!$socket) {
    die("Error: $errstr ($errno)\n");
}

// Send data over the persistent connection
fwrite($socket, "Hello, server!\n");

// Receive response from the server
$response = fgets($socket, 1024);
echo $response;

// Close the persistent connection
fclose($socket);