PHP 8.5.0 Alpha 4 available for testing

Voting

: two plus two?
(Example: nine)

The Note You're Voting On

MrStonedOne
10 years ago
You can use shift/unshift and push/pop to dequeue/undequeue and queue/unqueue respectively. Really handy for those applications that use sockets where you might not know you can't send data until you attempt to.

for example, this is a function for an application that will un-dequeue the remainder of the data if socket_write indicated it did not write the entire contents of the provided data.

<?php
function processSendQueue($socket, $sendQueue) {
while (!
$sendQueue->isEmpty()) {
//shift() is the same as dequeue()
$senditem = $sendQueue->shift();

//returns the number of bytes written.
$rtn = socket_write($socket, $senditem);
if (
$rtn === false) {
$sendQueue->unshift($senditem);
throw new
exception("send error: " . socket_last_error($socket));
return;
}
if (
$rtn < strlen($senditem) {
$sendQueue->unshift(substr($senditem, $rtn);
break;
}
}
}
?>

<< Back to user notes page

To Top