On which moment PHP executes an include on a switch?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

On which moment PHP executes an include on a switch?

Post by lovelf »

Code: Select all

<?php switch(browser_detection(browser)) {
case "msie6":
include_once("hvarie6.php");
break;
case "mozilla":
$fie6a= "";
$fie6b= "";
$fie6c= "";
$fie6d= "";
$fie6e= "";
$fie6f= "";
$fie6g= "";
?>
I would like to know if the php include occurs only for case msie6 or if the file is included anyway, the code above is just an example, I use many includes on switch statements and I am curious whether PHP actually includes the file for any case regardless of the call being placed for a certain case on the switch.

Thanks
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: On which moment PHP executes an include on a switch?

Post by Mark Baker »

One solution would be to test it and see; but I'll save you the effort.

PHP only includes the file when the case condition matches
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: On which moment PHP executes an include on a switch?

Post by lovelf »

But how could I test it?

The file might be included anyway from the server and not used for a certain case.

Thanks.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: On which moment PHP executes an include on a switch?

Post by Benjamin »

lovelf wrote:But how could I test it?

The file might be included anyway from the server and not used for a certain case.

Thanks.
Use require()
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: On which moment PHP executes an include on a switch?

Post by Mark Baker »

lovelf wrote:But how could I test it?.
A.php

Code: Select all

 
<?php echo 'A'; ?>
 
B.php

Code: Select all

 
<?php echo 'B'; ?>
 
C.php

Code: Select all

 
<?php echo 'C'; ?>
 
Main.php

Code: Select all

 
$test = 'B';
switch ($test) {
   case 'A':
      include('A.php');
      break;
   case 'B':
      include('B.php');
      break;
   case 'C':
      include('C.php');
      break;
}
 
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: On which moment PHP executes an include on a switch?

Post by lovelf »

Thanks Mark, I got it now.
Post Reply