Jump to content

require()/include() and return


timbrown

Recommended Posts

Hi,

 

I am trying to reuse code using include files. I understand that the files are evaluated seperatley from the rest of the code. The problem I am having is that when I use return, it does not affect the containing document, only the included one. example:

 

in this case the echo is not called because of the return:

 

return false;
echo "hello";

 

in this case the echo is called because the return is in the include:

 

"includeFile.inc":

return false;

 

container file:

require("includeFile.inc");
echo "hello";

 

Is there any way round this functionality? I would like to be able to just put code into a text file and have it reused in the normal flow of the code.

 

Thanks in advance,

 

Tim.

 

 

Link to comment
Share on other sites

Handling Returns: It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function. This is not, however, possible when including remote files unless the output of the remote file has valid PHP start and end tags (as with any local file). You can declare the needed variables within those tags and they will be introduced at whichever point the file was included.

 

include/require() are equivalent to having the code they contain right in your current file.  They are privy to the same scope as the code would be were it directly in your file.  You don't need to use a return at the end of your include()'d page.

 

You can use eval() to create and execute code at runtime, but I don't think that's what you want.

Link to comment
Share on other sites

Is there an alternative to include(), one that is compiled & evaluated at the same time as the rest of the code?

 

Code included into a file is evaluated as if it was actually typed into the original script in the place it is included. It is not parsed seperately to the rest of the code. Im not sure your using return in the right context, there usually isn't too much need for return outside of functions.

Link to comment
Share on other sites

Also, I assume you were using them, but if you weren't PHP tags are necessary within the included file:

 

When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

Link to comment
Share on other sites

This is literally what i'm trying to do -

 

function validate(){

 // this bit of code I want to reuse in several functions
 $this->log[] = __FUNCTION__.": an error occurred";
 return false;

}

 

This works fine, but once I put it into an include file, __FUNCTION__ does not work at all and return does not halt the function and return false as I would expect.

Link to comment
Share on other sites

__FUNCTION__ seems to work fine from within a method.

 

From the php manual -

 

If called from within a function, the return() statement immediately ends execution of the current function...

 

This does not seem to apply when using include() and also eval(). I suppose this is because eval() it is not actually being called from within a function.

 

Any other ideas?

Link to comment
Share on other sites

This does not seem to apply when using include()

 

Yes it does. Sorry but what your describing does not make sense. This will work as expected.

 

inc.php

<?php

  function foo() {
    return FALSE;
  }

?>

 

index.php

<?php

  include 'inc.php';

  if (!foo()) {
    echo 'foo() returned false';
  }

?>

Link to comment
Share on other sites

This does not seem to apply when using include()

 

Yes it does. Sorry but what your describing does not make sense. This will work as expected.

 

inc.php

<?php

  function foo() {
    return FALSE;
  }

?>

 

index.php

<?php

  include 'inc.php';

  if (!foo()) {
    echo 'foo() returned false';
  }

?>

 

That's because in my scenario the include is within the function. This "return" does not end execution of the function -

 

includefile.inc

<?php
echo __FUNCTION__;
return false;
?>

 

index.php

<?php
function(){
include("includefile.inc");
echo "hello";
}
?>

 

I want to be able to reuse includefile.inc in several methods.

Link to comment
Share on other sites

I don't think it's a bug.  The manual says that you CAN return a value from an include()'d file.

 

Like I said, you could use the return outside of the include file.

 

<?php
if ($error) {
$func = __FUNCTION__;
return include 'error.inc';
}

// error.inc:

$this->log[] = "$func: an error occurred";
return false;
?>

 

That will return from the function.  However, the two returns are redundant.  It might be easier to write an error logging method, keep that in a common functions or methods included file, and call it by passing __FUNCTION__.

 

<?php
// In an included file:
function log_error ($this,$func,$message='') {
$this->log[] = "$func: Error.  $message";
return false;
}

// In your program:

if ($error) return $this->log_error(__FUNCTION__);
?>

 

I don't know how many times you're using this, but you may not save so much code by using lots of includes.  But you're probably doing so to make it easier to change the error logging, right?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.