The return value of include

PHP never stops surprising me. I just found out that you’re able to return values from the inclusion statements (require, require_once, include and include_once) through an example of Zend_Config.

That means you’re able to write a config.php-file like this:

return array(
  'hostname' => 'localhost',
  'database' => 'test',
  'username' => 'test',
  'password' => 'test',
);

And use it like this:

$config = include 'config.php'; // $config is now the array
var_dump($config);

A side not here is also that PHP support dangling commas. I always leave the last comma in the array so I don’t forget to add it when I add a new line of configuration.

12 Comments

Jordi Boggiano  on November 28th, 2008

The last comma thingy is also useful when using SVN or other VCS tools, because if you add a line to the array the changeset is only that one line and not the last and the one before it, since you don’t have to add a comma to that one.

Ivo  on November 28th, 2008

This shouldn’t be surprising, it’s in the manual. :)

PaulG  on November 28th, 2008

Thats neat, thanks for showing me that.

William Notowidagdo  on November 28th, 2008

That suprise me. Thanks for sharing.

Knut Urdalen  on November 28th, 2008

@Ivo: I know, I first thought it was an undocumented feature since these functions doesn’t have a “return” section in the PHP manual (like other functions). But as usual I double check it and after reading the include-section more carefully I found an example that showed it. To most PHP developers I think it’s surprising ;)

Dave  on November 28th, 2008

I’ve been using it for a while - useful for caching and autoload include files etc. It shouldn’t be surprising considering that include et al execute the PHP script, so you could include an entire script and have a single return statement or create a report scaffold and include a particular report body. Just remember to be careful though; if you suppress errors from it using @include then you can get “funny” behaviour (and no compile errors!) so if you intend to get an array or object - check the type (that one caught me a few times!) :)

smialy  on November 28th, 2008

this have one bad things - is’t very slow ;)

Knut Urdalen’s Blog: The return value of include : WebNetiques  on November 29th, 2008

[...] a new entry Knut Urdalen looks at something that some PHP developers might have forgotten about - the return [...]

Knut Urdalen’s Blog: The return value of include : Dragonfly Networks  on November 29th, 2008

[...] a new entry Knut Urdalen looks at something that some PHP developers might have forgotten about - the return [...]

kevin  on November 30th, 2008

Yup, for your example though I’d rather use parse_ini_file().

Knut Urdalen  on November 30th, 2008

@kevin: This was just an example to prove a less known feature in PHP ;) INI-files is fine in many cases (easy to read as well as you don’t have to know PHP) so it can easily be managed by others lowering the risk of screwing up.

About INI-files: often I write apps that should be able to be hosted many places and it’s good practice to name you file config.ini.php instead of config.ini to ensure your configuation is executed as a PHP-script and is not available as clear text. A lot of PHP developers still have their ini-files available to the public…

Matt K  on December 1st, 2008

You can also use it in a function situation.

mysqli::connect(include ‘config.php’);

I’ve used it both ways and have had ok results. Can’t say I would use this all the time, but it’s useful when in a tight bind.

Leave a Comment