Forum Moderators: coopster

Message Too Old, No Replies

when an include() goes inside an if().

         

httpwebwitch

8:58 pm on May 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



consider the following:

if(false){
include("a.php");
}


in older versions of PHP, I concluded that the contents of "a.php" were being read from disk, and parsed. The code may as well have been assembled into the script before execution; even though nothing inside a.php would do anything because it's inside the if().

I'm running PHP5 now, and it seems like this isn't true any more. The code above doesn't not complain, even though "a.php" doesn't exist.

Reason I'm asking, is that I have a huge library of large helper functions, and I'd like to include them only if they're needed. I know whether they're needed based on the request.

so, imagine this:

<?php

switch($_GET['a']){
case "1":
include("1.php");
case "2":
include("2.php");
case "3":
include("3.php");
case "4":
include("4.php");
case "5":
include("5.php");
case "6":
include("6.php");
case "7":
include("7.php");
}
?>


would that be an effective way to marshall out large chunks of code?

Matthew1980

9:22 pm on May 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there httpwebwitch,

Yep! either that or a chain of if/elseif/else.

If you are going to go the switch way, you will need a default clause for handling an exception if the requested file isn't what you are expecting, or put a ternary on the $_GET['a'];

Ie:-

$data = (isset($_GET['a']) ? strip_tags($_GET['a']) : 'Call_this_what_you_like');

switch($data){
case "1":
include("1.php");
break;
case "2":
include("2.php");
break;
case "3":
include("3.php");
break;
case "4":
include("4.php");
break;
case "5":
include("5.php");
break;
case "6":
include("6.php");
break;
case "7":
include("7.php");
break;
}

You only 'stack' the case's if they are going to be handled the same way.. Though I am sure you knew that!

Interesting as you are having this issue though, I run similar things (php5.3.1) and still get the same output, and yes, the script would only throw an error if the path wasn't true or file didn't exist when the execution of the clause was proved true or false ( if(!), if(true)) at least that's the way I have always understood it to be.

Hope that helps a little ;)

Cheers,
MRb

httpwebwitch

10:16 pm on May 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



that's excellent news.

What I want to avoid is including many thousands of lines of modular code that will never be used. Even if it's not executed, it would still be parsed, and I need all the speed I can squeeze out of this script because it's being run many thousands of times per second, on several load-balanced servers :O

my bad for forgetting the switch breaks :)

coopster

11:59 am on May 19, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I often use arrays to to handle this type of logic. It will execute faster than rolling through all the switch control logic.
<?php 
$includes = array(
'1' => '1.php',
'2' => '2.php',
'3' => '3.php',
'4' => '4.php',
'5' => '5.php',
'6' => '6.php',
'7' => '7.php'
);
// scrub $data first, as Matthew1980 mentioned and then use it ...
if (isset($includes[$data])) {
include $includes[$data];
} else {
// handle otherwise
}
?>

httpwebwitch

5:04 pm on May 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LOL coopster, I already did exactly that. You know what they say about great minds.

And it's working great. Thanks!

I swear, this behaviour has changed subtly since PHP4. I didn't imagine it.