Return from Include File

Return from include file

includeme.php:

$x = 5;
return $x;

main.php:

$myX = require 'includeme.php'; 

also gives the same result

This is one of those little-known features of PHP, but it can be kind of nice for setting up really simple config files.

How to return an included file in PHP without using a return

Your problem is that using include essentially treats your template.php file as a single giant "echo" because there is no value returned inside template.php

You could change the template.php file to return a HTML string, and it would achieve what you want, but this is a nightmare to look at and manage.

return '<div>
<br/>' . $data . '<br/>
</div>';

An alternative, is to "capture" the output of the included file. Its a lot nicer on your template.php file and achieves the same thing. The output buffer functions essentially capture any information "echoed" - you need to be careful using this and the placement of your session_start however as it can catch you off guard depending on how your application is bootstrapped.

This is done by using three ob_ functions the main one being ob_get_contents which will be the contents of template.php in the example below.
http://php.net/manual/en/function.ob-get-contents.php

ob_start();
include $_SERVER['DOCUMENT_ROOT'] . 'template.php';
$html = ob_get_contents();
ob_end_clean();

This was your template files stay nice and clean, and the contents are all captured for returning in your json format.

Skip the rest of the (included) file in PHP

Just do return; or return($value); on top level of the inner.php file.

If called from the global scope, then
execution of the current script file
is ended. If the current script file
was include()ed or require()ed, then
control is passed back to the calling
file. Furthermore, if the current
script file was include()ed, then the
value given to return() will be
returned as the value of the include()
call.

Include a php file, but return output as a string instead of printing

You can use php buffers like this:

<?php
ob_start();
include('other.php');
$script = ob_get_contents(); // it will hold the output of other.php
ob_end_clean();

EDIT: You can abstract this into a function:

function inlcude2string($file) {
ob_start();
include($file);
$output = ob_get_contents(); // it will hold the output of other.php
ob_end_clean();
return $output;
}

$str = inlcude2string('other.php');

PHP return data from anonymous in file

So the relevant documentation can be found on the include document and the anonymous function document.

To summarise, included / required files can return values. These can then be assigned to variables.

In this case you could do the following:

$callable = require 'my-file.php';

This would assign the anonymous function to $callable at which point you can treat it as a standard closure (because that's what it is) and call it as you would a function:

$callable($anyargs, $needed);

Which in this case would return the data in the array.

Is it common to have just a return statement in a php file

No it isn't very common to have just a return statement, but it is used sometimes to store the configuration information in a separate config.php file so that the config can be included elsewhere with php require.

//config.php
<?php

return [
'app_key' => 'SomeRandomString',
'app_secret' => 'SomeRandomString',
];


// other-file.php
<?php

$config = require 'path/to/config.php';
$facebook = new Facebook($config['app_key'], $config['app_secret']);

How to get include contents as a string?

ob_start();
include 'test.php';
$string = ob_get_clean();

I think is what you want. See output buffering.



Related Topics



Leave a reply



Submit