by performance reasons we should use only realpath() instead of realpath() + file_exists() when checking existence of a file or directory ??
CASE A ::
if(realpath(__DIR__."/../file.php")===false)
CASE B ::
if(file_exists(realpath(__DIR__."/../file.php"))===false)
i think CASE A
do the job, and CASE B
do the job two times.
Not only is case B redundant (as realpath returns false if the path cannot be resolved or the file does not exist as per the docs), if the file does not exist, that is a bit silly.
Since this statement will return FALSE
:
realpath(__DIR__."/../file.php");
This:
file_exists(realpath(__DIR__."/../file.php"));
Is really this:
file_exists(FALSE); //!
realpath
will never return a "FALSY" value. By this I mean that it will never return something which == FALSE
but does not === FALSE
(eg. NULL
, ''
, 0, array()). Why? Well, the real path will always include a reference to the root — /
in *nix systems (Mac, Unix, Linux) and C:\
in Windows, and those two strings will evaluate to true when used as a boolean (say in an if, while, or for loop). This means you can just do:
if(!realpath(__DIR__."/../file.php")) // do something
Or, if you need to actually have the realpath, you can:
if(!($path = realpath(__DIR__."/../file.php")))
// file does not exist
else
// $path is now the full path to the file
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