Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
How to add foreach inside switch php

anyone know how to this?

switch ($page) {
  foreach ($getcustpage as $k => $v) {
   case $v:
    print 'Variable $v tripped switch: '.$k.'<br>'.$v;
   break;
  }
   case "result":
    //result
   break;
   default:
    //default 
   break;
}

I want to create a custom page from my settings. So the page will be available to access. anyone can help me?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I think you should break out your for loop outside of the switch. Now it is incorrect syntax. Try this:

foreach ($getcustpage as $k => $v) {
     switch ($k) {
   case $v:
    print 'Variable $v tripped switch: '.$k.'<br>'.$v;
   break;
   case "result":
    //result
   break;
   default:
    //default 
   break;
    }
}
over 4 years ago · Santiago Trujillo Report

0

I found the solution.

First i create normal switch like this

switch ($page) {
   case "result":
    //result
   break;
   default:
    //default 
   break;
}

Then i need to add a logic in default switch

if (in_array($page, $getcustpage)) {
  foreach ($getcustpage as $k => $v) {
    print 'Variable $v tripped switch: '.$k.'<br>'.$v;
  }
}else{
   print $page;
}

Thanks for help.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!