Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

require only if?

Status
Not open for further replies.

sirugo

Programmer
Joined
Aug 1, 2000
Messages
162
Location
SE
I have a script that looks like this:

// intro-code

if(blah blah)
{
// a lot of code
}
elseif(blah blah)
{
// a lot of code
}
elseif(blah blah)
{
// a lot of code
}
.
.
.
else
{
// a lot of code
}

So each if-statement requires a lot of code which means that all the content of the file must be compiled although only some of the code will be used.
Is there a way to use any function etc that only compiles the code that will be used at a given status of the if-statement? For example "require"?
Will the code in the required file only be read/compiled if it actually enters the given if-section?
Like this:

if(blah blah)
{
require("thisone.php");
}
elseif(blah blah)
{
require("theotherone.php");
}


Any other suggestions?
 
You can conditionally inclue or require, I'd suggest an include because it can't give fatal errors - but otherwise works like a require.
Code:
include fileName

or even better:
Code:
include_once fileName

I can't think of any other way of doing it, but the require_once or include would save you from re-loading the same file twice if a function is reused in a latter conditional block.
 
OK, but will the script load the required file although it will never need it?

If I put the code there instead I guess everything will be compiled but if I require the files - will they ever be compiled if they are not needed?
 
sirugo,

My two cents:

That condition you're testing on, is it the same $var? If so, then the switch statement will a much better solution for you. Also, that {/* a lot of code */} you run conditionally -I'd make that into (one or more) function(s) instead just to give a better overview.

Good Luck §;O)


Jakob
 
Code:
//a lot of code
I think is a stand in for the a large function call that he wants to move to seperate files. I don't know about require, but an include inside a conditional will only execute (that is the file will only be loaded) if the condition is true. Also, PHP isn't a compiled language it's an interpeted languange... The code that is send weather in the same file or included from another will result in the same HTML being sent over the net. The disk access for the a single large (not huge) file is often less than the over head of opening a second file (as the hard drive would have to seek to the begining of the other file in opening 2 files, where the contents of a single would be kept together in the same secotor/block). I think your better off making them functions and moving them to the top/bottom of the file that they are called in unless they are used in another file or the
Code:
//alot of code
is really huge.

dkdude regardless of weather he uses a nested if/else
(read:
Code:
if(){}
else if(){}
else if(){}
-- and yes that is a nesting, as it's shorhand for
Code:
if()
{
}
else{ 
 if(){}
 else{ 
  if(){}
 }
}
)
or a switch the logic is the same, and the over head is equvilent. The if/else if has several advantages to the switch and writing the code as being nested. Over the switch it offers better readbility, less chance of error (forgetting a break) and better flow of control (break; is considered bad code style, as it is akin to a
Code:
goto
). Over the other way of writting it the nesting is readability.
 
You choose between [tt]if[/tt] and [tt]switch[/tt] depending on the shape and form of your needs. I can think of examples where [tt]if[/tt] is absolutely preferred over [tt]switch[/tt] and vice versa.

However, here's two somewhat important differences to consider:

First difference - Quote from "SAMs Teach Yourself PHP in 24 hrs, 2nd. edition"

The result of an expression evaluated as part of an [tt]if[/tt] statement is read as either true or false.

The expression of a [tt]switch[/tt] statement yields a result that is tested against any number of values.


Second difference - Quote from php.net's online manual

In a [tt]switch[/tt] statement, the condition is evaluated only once and the result is compared to each case statement. In an [tt]elseif[/tt] statement, the condition is evaluated again. If your condition is more complicated than a simple compare and/or is in a tight loop, a [tt]switch[/tt] may be faster.

I don't know how to compare [tt]break;[/tt] and [tt]goto[/tt]. Doesn't make sense to me -different languages; different purpose §;O)

Remembering the [tt]break;[/tt] is a given when you use the [tt]switch[/tt] statement. I realize that there can be a loophole if you forget it, so it is (as always) important to test you code thoroughly.

Having said that, you can in fact leave out the [tt]break;[/tt] to serve special needs.

Consider this example

Code:
function switchStatement($country) {
  switch($country) {
    case "Ireland" : 
    case "US" : 
    case "Australia" : 
    case "New Zealand" : 
    case "Canada" : 
    case "UK" : 
      return "Hello!";
    break;
    case "Belgium" : 
    case "Morocco" :
    case "Ivory Coast" :
    case "France" :
      return "Bon jour!";
    break;
    case "Denmark" : 
      return "Hej!";
    break;
    default : 
      return "How do I greet you?";
  }
}

Which, could look like this using an [tt]if/elseif/else[/tt] construction instead:

Code:
function ifStatement($country) {
  if($country=="Ireland")
    return "Hello!";
  elseif($country=="US")
    return "Hello!";
  elseif($country=="Australia")
    return "Hello!";
  elseif($country=="New Zealand")
    return "Hello!";
  elseif($country=="Canada")
    return "Hello!";
  elseif($country=="UK")
    return "Hello!";
  elseif($country=="Belgium")
    return "Bon jour!";
  elseif($country=="Morocco")
    return "Bon jour!";
  elseif($country=="Ivory Coast")
    return "Bon jour!";
  elseif($country=="France")
    return "Bon jour!";
  elseif($country=="Denmark")
    return "Hej!";
  else
    return "How do I greet you?";
}

Maybe not the brightest and most useful example, but it serves this purpose well, I recon. This is also an example how a [tt]switch[/tt] statement can give (in this case & in humble opinion) better overview than the [tt]if[/tt] statement. Surely you can code it differently, but I think you get the general idea.

Finally, combining the two will sometimes make sense too.

Best Regards

dkdude1.gif


Jakob
 
OK, thanks

But I know how to handle if- and switch-statements.

My question is still:

Will the required file be interpreted although a given if-statement will not equal true?
 
No, if the condition isen't true, the next statement -or block- will not be interpreted!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top