PHP 8.5.0 Alpha 1 available for testing

Voting

: nine plus zero?
(Example: nine)

The Note You're Voting On

e-vela at bol dot com dot br
7 years ago
Usage example for MSG_PEEK: this function tells if the socket has data available to be read, but preserving it to be read at a future moment.

<?php
// Workaround for the missing define
if(!defined('MSG_DONTWAIT')) define('MSG_DONTWAIT', 0x40);

// Function to check if there is data available in the socket
function SocketHasData($socket) {
// Based on the following fact:
// $result=0 -> disconnected, $result=false -> no data

$data = ''; // We need a buffer, but we won't use it

// MSG_PEEK means to preserve data in the queue, so it can
// actually be read afterwards
$result = socket_recv($socket, $data, 1, MSG_PEEK | MSG_DONTWAIT );

if (
$result === false) return false; // If no data, returns false
return true; // Otherwise returns true
}
?>

<< Back to user notes page

To Top