Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6173509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:35:21+00:00 2026-05-23T23:35:21+00:00

As taken from https://stackoverflow.com/questions/4891301/top-bad-practices-in-php Is this similar code killing kittens, too? foreach (file(longFile.txt) as

  • 0

As taken from https://stackoverflow.com/questions/4891301/top-bad-practices-in-php

Is this similar code killing kittens, too?

foreach (file("longFile.txt") as $line) {
    // ouch! Kitten killed
}

??

For those who have no idea what am I talking about:

Is PHP getting longFile.txt everytime it goes to next line of file or no? Talking about this code:

foreach (file("longFile.txt") as $line) {
        // something
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:35:22+00:00Added an answer on May 23, 2026 at 11:35 pm

    In the linked question the for loop incurs a performance hit by calling count on every iteration. foreach uses internal pointers to iterate through the array passed to it.

    In your example file() will be called once and the resulting array will be passed to foreach which will iterate through it, thus the kittens are saved. Happy Caturday!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is a code snippet taken from https://forums.embarcadero.com/message.jspa?messageID=219481 if FileExists(dstFile) then begin Fs :=
This question comes from my experience with the following question: https://stackoverflow.com/questions/492748/new-responses-icon-on-so-crashes-ie7-closed In that question,
I've referred to this very good reference: https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more but I'm having some very serious
Check this URL: http://api.stackoverflow.com/1.1/users/811785/questions?sort=votes It's a URL from StackOveflow's API. The returned JSON is
I have the following piece of code taken from the PHP manual on the
I use this code which is taken from MVC futures and attach the Attribute
Following is the code snippets taken from http://pietschsoft.com/post/2011/09/09/Tag-Editor-Field-using-jQuery-similar-to-StackOverflow.aspx // pre-selected tags values: [ 'javascript',
I started to take a look at the following question: https://stackoverflow.com/questions/8616458/sql-query-like-clarification Unfortunately it appeared
I found and followed an example from Stackoverflow (http://stackoverflow.com/questions/2310139/how-to-read-xml-response-from-a-url-in-java) of how to read an
This is inspired by/taken from this thread: http://www.allegro.cc/forums/thread/603383 The Problem Assume the user gives

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.