PHP include/require within a function

故事扮演 提交于 2020-01-03 08:50:26

问题


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

I am looking to do this as I have lots of functions in separate files and they all have a large chunk of shared code at the top.

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

file.php:

...
return "Something";

At the moment the return something appears to break out of the include_once and not the sync function, is it possible for the included file's return to break out?

Sorry for the slightly odly worked question, hope I made it make sense.

Thanks,


回答1:


You can return data from included file into calling file via return statement.

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 return statement within calling script. return works only within current scope.

EDIT:

I am looking to do this as I have lots of functions in separate files and they all have a large chunk of shared code at the top.

In this case why don't you put this "shared code" into separate functions instead -- that will do the job nicely as one of the purposes of having functions is to reuse your code in different places without writing it again.




回答2:


return will not work, but you can use the output buffer if you are trying to echo some stuff 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;
}



回答3:


I don't think it works like that. The include does not simply put the code in place, it also evaluates it. So the return means that your 'include' function call will return the value.

see also the part in the manual about this:

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.

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

The manual has an example (example #5) that shows what 'return' does:

Simplified example:

return.php

<?php  
$var = 'PHP';
return $var;
?>

testreturns.php

<?php   
$foo = include 'return.php';
echo $foo; // prints 'PHP'
?>



回答4:


I think you're expecting return to behave more like an exception than a return statement. Take the following code for example:

return.php:

return true;

?>

exception.php:

<?php

throw new exception();

?>

When you execute 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) {}

?>

...you get the following output as a result:

Executing testReturn()...testReturn() executed normally.

Executing testException()...

If you do use the exception method, make sure to put your function call in a try...catch block - having exceptions flying all over the place is bad for business.




回答5:


Rock'n'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');

output

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

just show me how

like this :

return (include 'bar.php');

Have a good day !



来源:https://stackoverflow.com/questions/6402013/php-include-require-within-a-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!