Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include/require within a function

Tags:

php

return

break

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,

like image 581
Pez Cuckow Avatar asked Jun 19 '11 11:06

Pez Cuckow


People also ask

How we use include () and require () function in PHP?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

What are the difference between include () and require () file in PHP?

include() Vs require() The only difference is that the include() statement generates a PHP alert but allows script execution to proceed if the file to be included cannot be found. At the same time, the require() statement generates a fatal error and terminates the script.

Are include () and require () similar functions?

These functions are the same if but they have one difference. The difference is that the include() function produces a warning, but the script will continue execution, while the require() function produces a warning and a fatal error i.e. the script will not continue execution.

What is difference between include () require () and require_once ()?

They are all ways of including files. Require means it needs it. Require_once means it will need it but only requires it once. Include means it will include a file but it doesn't need it to continue.


2 Answers

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.

like image 173
LazyOne Avatar answered Sep 20 '22 06:09

LazyOne


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;
}
like image 35
Ibu Avatar answered Sep 20 '22 06:09

Ibu