What potential pitfalls should be considered when using PHP's flock() function to lock a file?

One potential pitfall when using PHP's flock() function to lock a file is the possibility of deadlock if multiple processes are attempting to lock the same file. To avoid this, it's important to always release the lock after it's no longer needed. Additionally, be mindful of the type of lock being used (shared or exclusive) to prevent conflicts between processes.

$fp = fopen("example.txt", "r+");

if (flock($fp, LOCK_EX)) {
    // Perform operations on the locked file
    flock($fp, LOCK_UN); // Release the lock
} else {
    echo "Could not lock the file!";
}

fclose($fp);