Home > Article > Backend Development > PHP determines whether a file exists. Detailed explanation of the use of the file_exists() function
Introduction: When writing a program, I found that there are two ways of writing when judging whether a file exists. Some people use is_file, and some people use file_exists. Which one is better or more suitable? To determine the existence of a file, use is_file or file_exists? When writing a program, I found that there are two methods to determine whether a file exists. However, the performance of these two functions is different. is_file() is faster than file_exists(). point.
<?php // 运行 file_exists 10000 次 $time = microtime(); $time = explode(' ', $time); $begintime = $time[1] + $time[0]; for($i=0;$i<10000;$i++) file_exists('/Users/Jacky'); // 文件存在 $time = microtime(); $time = explode(" ", $time); $endtime = $time[1] + $time[0]; $totaltime = ($endtime - $begintime); echo '运行file_exists 10000 次所花时间: ' .$totaltime. ' 秒'.PHP_EOL; // 运行 is_file 10000 次 $time = microtime(); $time = explode(" ", $time); $begintime = $time[1] + $time[0]; for($i=0;$i<10000;$i++) is_file('/Users/Jacky'); $time = microtime(); $time = explode(" ", $time); $endtime = $time[1] + $time[0]; $totaltime = ($endtime - $begintime); echo '运行 is_file 10000 次所花时间: ' .$totaltime. ' 秒.'.PHP_EOL;Second test, the file does not exist
<?php // 运行 file_exists 10000 次 $time = microtime(); $time = explode(' ', $time); $begintime = $time[1] + $time[0]; for($i=0;$i<10000;$i++) file_exists('/Users/Jackys'); // 文件不存在 $time = microtime(); $time = explode(" ", $time); $endtime = $time[1] + $time[0]; $totaltime = ($endtime - $begintime); echo '运行file_exists 10000 次所花时间: ' .$totaltime. ' 秒'.PHP_EOL; // 运行 is_file 10000 次 $time = microtime(); $time = explode(" ", $time); $begintime = $time[1] + $time[0]; for($i=0;$i<10000;$i++) is_file('/Users/Jackys'); $time = microtime(); $time = explode(" ", $time); $endtime = $time[1] + $time[0]; $totaltime = ($endtime - $begintime); echo '运行 is_file 10000 次所花时间: ' .$totaltime. ' 秒.'.PHP_EOL;
The above is the detailed content of PHP determines whether a file exists. Detailed explanation of the use of the file_exists() function. For more information, please follow other related articles on the PHP Chinese website!