Foreach cycle inside the enclosure

I am trying to break into the PHP pool and I am facing a problem - I am trying to set up a way that I can simply add a new record to the array and this will make a new page so that someone can access.

This is the code:

 switch ($page) {

 // Adds the links.
 foreach ($LINKS as $linkName => $linkAdd) {
   case $linkName:
     require "templates/views/$linkAdd";
 }

 // The default switch.
 default:
   echo '404 Not Found';
 break;

 }

The loop gives me an error saying that it was unexpected to use the loop foreachinside switch, I was wondering if there was a way around this or if it just cheated not to use this method?

Is this just the loop foreachthat is executed by this method? Or there are other types of loops for this.

+4
source share
2 answers

foreach . . . $Links $page , .

if(isset($LINKS[$page])) { // check if $page exists in $LINKS
    require "templates/views/" . $LINKS[$page]; // include
} else {
    echo '404 Not Found'; // default case
}
+5

foreach , case-Statement ( ).

foreach , array_key_exists():

if (!array_key_exists($page, $LINKS)) {
    // Default case
    echo '404 Not Found';
    exit;
}

require_once __DIR__ . '/templates/views' . $LINKS[$page];

:

  • .
  • (, __DIR__).
  • ( , $LINKS ).
0

All Articles