PHP 8.3.4 Released!

socket_clear_error

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

socket_clear_error清除套接字或者最后的错误代码上的错误

说明

socket_clear_error(?Socket $socket = null): void

这个函数清除给定的套接字上的错误代码或是最后一个全局的套接字如果套接字没有指定的话。

这个函数允许明确的重置错误代码值 不论是一个套接字或者最后全局错误代码的扩展, 这对在检测应用的一部分是否有错误发生是十分有用的。

参数

socket

socket_create() 创建的 Socket 实例。

返回值

没有返回值。

更新日志

版本 说明
8.0.0 现在 socketSocket 实例, 之前是 resource
8.0.0 参数 socket 可以为 null。

参见

add a note

User Contributed Notes 2 notes

up
0
raphael at engenhosweb dot com dot br
12 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();
};
?>
up
-1
ludvig dot ericson at gmail dot com
17 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.
To Top