pg.
pg.

Reputation: 2531

I want a PHP include to be interrupted

Something like this is in a file called "header.php" which all files on my site include:

switch (str_replace('/folder_name/','',$_SERVER['REQUEST_URI'])){
    case 'index.php':
    $page_title='main page';
    break;
    case 'page1.php':
    $page_title='page 1 is here';
    break;
    case 'page2.php':
    $page_title='this is page 2';
    break;
}

And it's followed by some HTML that's common to all the pages:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
etc... 

Can I make it so that, for one of the cases in the switch, the include stops right there? I am asking if there is a way to interrupt an include from within the include. So for example it might look like:

case 'page3.php':
$page_title='the title for page 3';
STOP_THE_INCLUDE
break;

Is this possible?

Upvotes: 0

Views: 103

Answers (2)

RobertSF
RobertSF

Reputation: 528

When you exit the switch, use an IF to avoid the rest of the include? Like this:

 $stopInclude = false;
 switch (str_replace('/folder_name/','',$_SERVER['REQUEST_URI'])){ 
   case 'index.php': 
     $page_title='main page'; 
     break; 
   case 'page1.php': 
     $page_title='page 1 is here'; 
     break; 
   case 'page2.php': 
     $page_title='this is page 2'; 
     break;
   case 'page3.php': 
     $page_title='the title for page 3'; 
     $stopInclude = true;
     break; 
 }
 if (!$stopInclude) {
   //whole rest of the include
 }

Upvotes: 1

user7675
user7675

Reputation:

Yes, you can simply return; inside an include.

Upvotes: 4

Related Questions