timbrown Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted April 19, 2007 Share Posted April 19, 2007 Is there any way round this functionality? No. This is the intention of return. Quote Link to comment Share on other sites More sharing options...
timbrown Posted April 19, 2007 Author Share Posted April 19, 2007 Thanks for getting back to me. Is there an alternative to include(), one that is compiled & evaluated at the same time as the rest of the code? Quote Link to comment Share on other sites More sharing options...
only one Posted April 19, 2007 Share Posted April 19, 2007 require_once(); Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
timbrown Posted April 19, 2007 Author Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted April 19, 2007 Share Posted April 19, 2007 Im not sure that __FUNCTION__ will work within a method (can't test at the moment), try __METHOD__ . Quote Link to comment Share on other sites More sharing options...
timbrown Posted April 19, 2007 Author Share Posted April 19, 2007 __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? Quote Link to comment Share on other sites More sharing options...
trq Posted April 19, 2007 Share Posted April 19, 2007 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'; } ?> Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 19, 2007 Share Posted April 19, 2007 return include 'error.inc'; // Where inc file contains a return false Quote Link to comment Share on other sites More sharing options...
timbrown Posted April 19, 2007 Author Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted April 19, 2007 Share Posted April 19, 2007 Ah, now I see what you mean, and yes, you are correct. Looks to me like a bug, either that or Im not understanding the docs. Id'e file a bug report and see what response you get. Quote Link to comment Share on other sites More sharing options...
timbrown Posted April 19, 2007 Author Share Posted April 19, 2007 Thanks, If YOU might not be understanding the docs, what chance does that give me!?!?! I will try and file a bug report and see how far I get. Thanks for all the help, Tim. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 19, 2007 Share Posted April 19, 2007 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? Quote Link to comment Share on other sites More sharing options...
timbrown Posted April 20, 2007 Author Share Posted April 20, 2007 Yeah I suppose I wouldn't save that much code, just trying to keep things neat and orderly. There are ways to achieve what I want, but I would prefer to just have an include. p.s. I tried reporting this as a bug but it isn't. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.