I'm including a custom.css file in my template to allow site owners to add their own css rules. However, when I ship the file, its empty and there's no sense loading it if they've not added any rules to it.
What's the best way to determine if its empty?
if ( 0 == filesize( $file_path ) ) { // file is empty } // OR: if ( '' == file_get_contents( $file_path ) ) { // file is empty }
PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true.
Answer: Use the PHP empty() function You can use the PHP empty() function to find out whether a variable is empty or not. A variable is considered empty if it does not exist or if its value equals FALSE .
is_null() The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL .
To delete a file in PHP, use the unlink function. Let's go through an example to see how it works. The first argument of the unlink function is a filename which you want to delete. The unlink function returns either TRUE or FALSE , depending on whether the delete operation was successful.
file_get_contents()
will read the whole file while filesize()
uses stat()
to determine the file size. Use filesize()
, it should consume less disk I/O and much less memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With