Phil Kurth
/ Tips /

Included files can return values which is a great way to manage app configurations.

Last updated • 22 October 2019

A neat feature of included files is the fact that they can return data. You can simply assign the output of an include or require statement to a variable and get to work on whatever is returned from that file.

Personally, I use this for configuration data where I have arrays full of options. I could load this all up in a class but by keeping it in a separate file, I keep the codebase that little bit cleaner and can store many configuration data sets in the one place. This is actually something I picked up whilst working with Laravel a while back and now implement in many of my projects including the plugin boilerplate that I’ve been crafting over the past few months.

Here’s a look at a file returning an array of data:

<?php

return [
	'some' => 'config',
	'data' => 'could',
	'go' => 'here'
];

And here’s the return value from that file being assigned to a variable:

<?php

$config = include 'config.php';

echo $config['some']; // 'config'

It’s a simple technique but I dig it.

Need a website?
Let's talk.

From website design & SEO through to custom WordPress plugin development. I transform ideas into dynamic, engaging, and high-performing solutions.

Phil Kurth, web designer and developer in Geelong