8

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,

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I don't think this is what was asked. The question seems to be "can I put a return inside the included file, that will act as if 'return' was called in my function. And this is not the case.
@Nanne: yeah .. was editing my answer straight away after posting
Unfortunately this is work for a friend to "must" have all of his project in annoying separate files :-(
@Pez Cuckow: OK -- you can try this one then (but it will only work if included file will ALWAYS use return statement): return include_once "include.php";. Apart of that -- advise your friend to refactor the code to use functions -- much better logic and less hassle.
beware that include_once or require_once will return true instead of the array if it is accessed a second time. This can be bad for example when loading config files with this strategy. use include or require instead unless you are sure it can only ever fire once.
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;
}

2 Comments

maybe someone can explain the -1, this is a viable method to fix the problem
I did not -1 myself, but I can explain: the "return will not work" at the beginning of the answer is just plain wrong. A return from the global scope of an included file will return a value that is used as return value from the include statement itself.
2

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

Comments

0

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.

Comments

0

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 !

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.