socket_clear_error
(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
socket_clear_error — 清除套接字或者最后的错误代码上的错误
说明
这个函数清除给定的套接字上的错误代码或是最后一个全局的套接字如果套接字没有指定的话。
这个函数允许明确的重置错误代码值 不论是一个套接字或者最后全局错误代码的扩展, 这对在检测应用的一部分是否有错误发生是十分有用的。
返回值
没有返回值。
更新日志
版本 | 说明 |
---|---|
8.0.0 |
现在 socket 是 Socket 实例,
之前是 resource。
|
8.0.0 |
参数 socket 可以为 null。
|
参见
- socket_last_error() - Returns the last error on the socket
- socket_strerror() - Return a string describing a socket error

User Contributed Notes 2 notes
raphael at engenhosweb dot com dot br ¶
10 years ago
You can do this too, with anonymous function:
<?php
$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or function() {
$errstr = socket_strerror(socket_last_error());
echo ("Failed to create socket: " . $errstr);
socket_clear_error();
};
?>
ludvig dot ericson at gmail dot com ¶
16 years ago
If you want to clear your error in a small amount of code, do a similar hack as to what most people do in SQL query checking,
<?php
$result = mysql_query($sql) or die(/* Whatever code */);
?>
It could look like this:
<?php
if (!($socket = socket_create(/* Whatever code */)) {
echo ("Failed to create socket: " . socket_strerror(socket_last_error()) and socket_clear_error());
}
?>
As you can see, I use "and" here instead of "or" since the first part will always return true, thus if you use or PHP's lazy boolean checking will not execute the last part, which it will with an and if the first part is true.
备份地址:http://www.lvesu.com/blog/php/function.socket-clear-error.php