How can microtime() function be used effectively in PHP for measuring script execution time?
To measure script execution time in PHP, the microtime() function can be used effectively. By capturing the current time at the beginning and end of the script, we can calculate the difference to get the total execution time. This can be useful for optimizing code and identifying performance bottlenecks.
// Start time
$start_time = microtime(true);
// Code to be measured
// ...
// End time
$end_time = microtime(true);
// Calculate execution time
$execution_time = $end_time - $start_time;
echo "Script execution time: $execution_time seconds";