What is the purpose of using substr_count and file_get_contents in PHP to count script lines?

The purpose of using substr_count and file_get_contents in PHP to count script lines is to efficiently count the number of lines in a file without having to read the entire file into memory. This can be useful for tasks such as analyzing code files or generating statistics on the number of lines in a file.

$file_path = 'example.txt';
$file_contents = file_get_contents($file_path);
$line_count = substr_count($file_contents, "\n") + 1; // Add 1 to account for the last line
echo "The file $file_path has $line_count lines.";