Is it possible to write a PHP script that processes commands through a socket connection and provides a response?

Yes, it is possible to write a PHP script that processes commands through a socket connection and provides a response. To achieve this, you can create a PHP script that listens on a specific port for incoming connections, reads the commands sent by the client, processes them, and sends back a response. This can be useful for creating custom server-client applications or implementing communication between different systems.

<?php

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, '127.0.0.1', 8888);
socket_listen($socket);

while (true) {
    $client = socket_accept($socket);
    $input = socket_read($client, 1024);
    
    // Process the command received from the client
    $response = "Response to command: " . $input;
    
    socket_write($client, $response, strlen($response));
    
    socket_close($client);
}

socket_close($socket);
?>