What is the best practice for handling errors and warnings in PHP functions like filesize?

When using functions like filesize in PHP, it is important to handle errors and warnings properly to avoid unexpected behavior or crashes in your application. One way to do this is by checking if the file exists before calling the filesize function and handling any potential errors that may occur.

$file = 'example.txt';

if (file_exists($file)) {
    $size = filesize($file);
    if ($size !== false) {
        echo "File size: " . $size . " bytes";
    } else {
        echo "Error getting file size";
    }
} else {
    echo "File does not exist";
}