Current Location: Home> Latest Articles> What Does the Return Value of socket_select in PHP Mean? A Complete Explanation of Each Return Result

What Does the Return Value of socket_select in PHP Mean? A Complete Explanation of Each Return Result

gitbox 2025-09-12

Return Value Explanation

1. > 0 —— Indicates socket status has changed

When the return value is greater than 0, the value represents how many sockets in the given array have experienced a status change.
For example:

  • There are 2 sockets ready for reading in the readable array;

  • There is 1 socket ready for writing in the writable array;
    Thus, the return value would be 3.

At the same time, the passed $read, $write, and $except arrays will be modified, retaining only those sockets that are ready, while others will be removed.
Therefore, you must check these arrays after the call, rather than relying on the original arrays.


2. 0 —— Timeout, no socket status change

If no socket has changed status within the specified timeout period, the return value will be 0.
This is typically used for non-blocking checks, such as timed polling.


3. false —— An error occurred

If the return value is false, it means the function call failed. You can usually use socket_last_error() to retrieve the error code and then use socket_strerror() to convert it into a readable error message.
Common causes include:

  • The provided array contains invalid sockets;

  • The parameter format is incorrect;

  • The system call failed.


Example Code

<span><span><span class="hljs-variable">$socket</span></span><span> = </span><span><span class="hljs-title function_ invoke__">socket_create</span></span><span>(AF_INET, SOCK_STREAM, SOL_TCP);
</span><span><span class="hljs-title function_ invoke__">socket_connect</span></span><span>(</span><span><span class="hljs-variable">$socket</span></span><span>, </span><span><span class="hljs-string">&#039;127.0.0.1&#039;</span></span><span>, </span><span><span class="hljs-number">8080</span></span><span>);
<p></span>$read = [$socket];<br>
</span>$write = null;<br>
</span>$except = null;</p>
<p></span>$changed = socket_select($read, $write, $except, 5);</p>
<p data-is-last-node="" data-is-only-node=""></span>if ($changed === false) {<br>
</span>echo "Error occurred: " . </span>socket_strerror(</span>socket_last_error()) . PHP_EOL;<br>
} </span>elseif ($changed === 0) {<br>
</span>echo "Timeout, no socket status change" . PHP_EOL;<br>
} </span>else {<br>
</span>echo "There are {$changed} sockets ready" . PHP_EOL;<br>
</span>// At this point, the $read array only contains sockets that are ready for reading<br>
}<br>
</span>