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

  • SEARCH
  • Home
  • 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 8662953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:51:42+00:00 2026-06-12T16:51:42+00:00

1st off I’m new to PHP. I have been using for loop,while loop,foreach loop

  • 0

1st off I’m new to PHP. I have been using for loop,while loop,foreach loop in scripts. I wonder

  • which one is better for performance?
  • what’s the criteria to select a loop?
  • which should be used when we loop inside another loop?

the code which I’m stuck with wondering which loop to be used.

for($i=0;$i<count($all);$i++)
{
 //do some tasks here
 for($j=0;$j<count($rows);$j++)
 {
  //do some other tasks here 
 }
}

It’s pretty obvious that I can write the above code using while. Hope someone will help me out to figure out which loop should be better to be used.

  • 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-06-12T16:51:43+00:00Added an answer on June 12, 2026 at 4:51 pm

    which one is better for performance?

    It doesn’t matter.

    what’s the criteria to select a loop?

    If you just need to walk through all the elements of an object or array, use foreach. Cases where you need for include

    • When you explicitly need to do things with the numeric index, for example:
    • when you need to use previous or next elements from within an iteration
    • when you need to change the counter during an iteration

    foreach is much more convenient because it doesn’t require you to set up the counting, and can work its way through any kind of member – be it object properties or associative array elements (which a for won’t catch). It’s usually best for readability.

    which should be used when we loop inside another loop?

    Both are fine; in your demo case, foreach is the simplest way to go.

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

Sidebar

Related Questions

1st I have to say I know nothing bout php. I was actually doing
1st off I am new to Delphi so this may be a mundane detail
I have been trying to implement 2 Seperate Contact Modals based off an original
I am very new to PHP and even more new to CakePHP, which is
I'm creating a simple questions DB, here are some details to start: 1st off,
1st disclaimers: I'm not a programmer, never was had never been taught higher math
This is the 1st time I am using stackoverflow, I usually find answers to
I have 2 tables: 1st holds employees (of ones in any position) and the
I have 1st popup, and this 1st popup opens 2nd popup Code: <view-state id=paneMaintenance
I've been off in Java land for about a month or so and now,

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.