Pez Cuckow
Pez Cuckow

Reputation: 14422

PHP include/require within a function

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,

Upvotes: 8

Views: 7492

Answers (5)

user1270589
user1270589

Reputation:

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 !

Upvotes: 0

Jacob Honeyhume
Jacob Honeyhume

Reputation: 1965

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.

Upvotes: 0

LazyOne
LazyOne

Reputation: 165228

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.

Upvotes: 7

Nanne
Nanne

Reputation: 64409

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'
?>

Upvotes: 2

Ibu
Ibu

Reputation: 43830

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;
}

Upvotes: 2

Related Questions