This works if the return value of socket_read() is "thruthy" or "falsy".
Falsy is '', 0, [], null or false.
Truthy is anything else.
if ( $data = socket_read($socket, 1024) ) {
     echo $data;
}
And if you want to be more specific you can even do the following (credits to @Benni):
if ( 'foo' === $data = socket_read($socket, 1024) ) {
    echo 'data equals foo';
}
Or
if ( is_array($data = socket_read($socket, 1024) ) {
    var_dump($data);
}
Your example might throw an exception if you are not sure socket_read() returns a string.
In that case you can do the following:
if ( is_string($data = socket_read($socket, 1024)) ) {
     echo $data;
}
For more information about PHP boolean behaviour see
https://www.php.net/manual/en/language.types.boolean.php