If you search for this topic in a search engine, most of them will tell you that the best way is:
count(explode(“\n”, $line));
But it’s not the most efficient. It’s actually
substr_count($line, “\n”);
On my workstation, running this 500,000 times on a line of 380 lines shows that substr_count executes in 19 seconds whereas count(explode()) combo takes 118 seconds.
Why? Probably because explode() is an additional step in memory usage.
Food for thought.
Related posts: