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

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'
];
view raw config.php hosted with ❤ by GitHub

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

<?php
$config = include 'config.php';
echo $config['some']; // 'config'
view raw app.php hosted with ❤ by GitHub

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.
Subscribe to get the latest insights & updates in the world of web and how it impacts your business and website.
© 2024 Phil Kurth  |  All rights reserved.