PHP includes / requires inside function

Is it possible to have return statements inside an included file that is inside a function in PHP?

I want to do this because I have many functions in separate files and they have a large piece of common code at the top.

As in function sync() { include_once file.php; echo "Test"; } 

file.php:

 ... return "Something"; 

At the moment, something appears that comes out of the include_once function, not the synchronization function, is it possible for the returned file to be returned?

Sorry for the slightly confusing question, hope I made it reasonable.

Thanks,

+7
source share
5 answers

You can return data from the included file to the calling file using the return .

include.php

 return array("code" => "007", "name => "James Bond"); 

file.php

 $result = include_once "include.php"; var_dump("result); 

But you cannot call return $something; and have it as a return statement in a script call. return only works within the current scope.

EDIT:

I want to do this because I have many functions in separate files and they all have a large piece of common code at the top.

In this case, why don’t you put this "common code" in separate functions instead - it will do the job nicely, since one of the goals of executing the functions is to reuse the code in different places without writing it again.

+6
source

return will not work, but you can use the output buffer if you try to repeat some things in your include file and return it somewhere else;

 function sync() { ob_start(); include "file.php"; $output = ob_get_clean(); // now what ever you echoed in the file.php is inside the output variable return $output; } 
+2
source

I do not think so. Enable not just put the code in place, it also evaluates it. So returning means that your call to the include function will return a value.

see also part of the manual on this subject:

Return processing: it is possible to execute the return () statement inside the included file in order to stop processing in this file and return to the script that called it.

The return statement returns the included file and does not insert a return statement.

manual has an example (Example # 5) that shows what "return" does:

A simplified example:

return.php

 <?php $var = 'PHP'; return $var; ?> testreturns.php <?php $foo = include 'return.php'; echo $foo; // prints 'PHP' ?> 
+2
source

I think you expect return be more like an exception than the return statement. Take the following code, for example:

return.php:

 return true; ?> 

exception.php:

 <?php throw new exception(); ?> 

When executing the following code:

 <?php function testReturn() { echo 'Executing testReturn()...'; include_once('return.php'); echo 'testReturn() executed normally.'; } function testException() { echo 'Executing testException()...'; include_once('exception.php'); echo 'testException() executed normally.'; } testReturn(); echo "\n\n"; try { testException(); } catch (exception $e) {} ?> 

... the result is the following result:

Executing testReturn () ... testReturn () runs fine.

Running testException () ...

If you use the exception method, be sure to put the function call in a try...catch if exceptions thrown everywhere are bad for business.

0
source

Rock and roll like this:

index.php

 function foo() { return (include 'bar.php'); } print_r(foo()); 

bar.php

 echo "I will call the police"; return array('WAWAWA', 'BABABA'); 

Exit

 I will call the police Array ( [0] => WAWAWA [1] => BABABA ) 

just show me how

like this:

 return (include 'bar.php'); 

Have a nice day!

0
source

All Articles