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 751591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:41:03+00:00 2026-05-14T14:41:03+00:00

In PHP, While using a switch case loop, can i use the for loop

  • 0

In PHP, While using a switch case loop, can i use the for loop for iterating the cases?
for example

switch .....

foreach($xyz as $abc)

{
 CASE:$abc
}

default;

UPDATE

I am fetching the value from DB, this value is name of table, by using “case” I want to execute a particular query according to the table name..

Is this possible?

  • 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-14T14:41:04+00:00Added an answer on May 14, 2026 at 2:41 pm

    You probably just want to put the switch statement into the foreach?

    foreach ($tables as $table) {
        switch ($table) {
            case 'table_one' :
                // do something here
                break;
            case 'table_two' :
                // do something here
                break;
            case 'table_three' :
                // do something here
                break;
            default :
                // do some error handling here
                break;
        }
    }
    

    Alternatively, a switch isn’t that easy to read, consider going away from a switch and using an array-powered if, especially if you could dynamically create what you want to do each case:

    $tables = array('table_one', 'table_two', 'table_three');
    if (in_array($table, $tables)) {
        // do something here
    } else {
        // do some error handling here
    }
    

    That’s a lot more readable, even if your array has a lot of elements.

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

Sidebar

Related Questions

I am using a PHP while loop in order to get data from a
How can I insert a newline while using error_log() in PHP? I tried to
If I use magic methods. While using reflection API, I can't investigate class properties..
I'm trying to display ul>li list using PHP while loop and MYSQL. My database
I am experiencing a fatal error while using artisan on the (fantastic) Laravel PHP
After using PHP for a while now, I've noticed that not all built-in PHP
I need to do a PHP while loop, but only if a variable is
I'm receiving this PHP error while trying to use the Normalizer class, specifically the
While using PHP cli in ubuntu i got apache as web server i cant
I have an alternating rows using PHP: while($i < $num) { $idphysicians = mysql_result($qPhysician,$i,idphysicians);

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.