Base PhP questions Why use Foreach over just a basic loop?

Posted on 2011-10-19
Last Modified: 2012-05-12
Hi Experts - I have been trying to learn some PHP to integrate some of our in-house applications with the major application used by our company (Written by others) - I have come across a few basic questions and I seem to be having a problem getting an answer to.

Most of our (ok, all) of our work revolves around getting data from a database and displaying it to the user - I have been using APEX for oracle, so I'm much more familiar with that than anything in PHP.

When I look at some of the reports that are written by the company, I notice when they are doing total counts of some of our tables (which are fairly large, 60-90 columns each with a few million rows per table) they are using a "Foreach" - (I will include a snippit of code where they are counting the days of the week an incident occurs)

Couldn't you just do this in the "main" while loop with an IF statement?(they use case)
What is the value add of doing the foreach?  Is it a "best practice"?

Is this a question you can answer with the provided information or do I need to provide more?



			$district =substr($row["rm4_report_district"],0,2);

			$da = substr($row["rm4_report_date"],6,2);

			$mo = substr($row["rm4_report_date"],4,2);

			$yr = substr($row["rm4_report_date"],0,4);

			$day_of_week = date("l", mktime(0, 0, 0, $mo, $da, $yr));

			if ($firstime == "Y"){

				$district_ar[$num] = $district;

				$firstime = "N";




			$day_of_week = strtolower($day_of_week);

			debug_write("---------report date------=".$row["rm4_report_date"]);

			debug_write("---------day of week after------=".$day_of_week);

			foreach ($district_ar as $key => $choice){

				if ($district == $choice){

					$count_array = $key;


					$num_of_off[$key] = $num_of_off[$key] +1;

					switch ($day_of_week)


						case "monday":

							$monday[$key] = $monday[$key] + 1;

							$total_monday = $total_monday + 1;


						case "tuesday":

							$tuesday[$key] = $tuesday[$key] + 1;

							$total_tuesday = $total_tuesday + 1;


						case "wednesday":

Open in new window

Question by:hej613
    LVL 38

    Expert Comment

    by:Aaron Tomosky
    The while loop is for each row. The for each is on the district array. So or each row it loops through the district array and matches some stuff. This looks really inefficient to me at first glance.
    LVL 1

    Author Comment

    Understand - I know very little about PHP programming, but I have had to re-write a lot of PL/SQL for oracle because everything is very "bloated"  - I would not doubt if the php is like that also...
    LVL 38

    Accepted Solution

    I frequently find in these situations that modifying the query to return the data they want instead of using php to loop a bunch of times will significantly speed thing up.
    LVL 107

    Assisted Solution

    by:Ray Paseur
    What Aaron said at ID:36994171!

    Here is the man page that describes foreach()

    And here is an article showing how to handle DATETIME values in PHP and MySQL.  Hint: You don't do it the way that programming shows!

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now