php language basics

Includes and File Organization

Modern applications usually use Composer autoloading for classes, but require, require_once, and returning arrays from PHP files still appear in real PHP projects and legacy code.

Loading A Helper File

helpers.php:

PHP example
<?php

declare(strict_types=1);

function formatProductLine(array $product): string
{
    return $product['name'] . ': ' . $product['price_cents'] . ' cents';
}

report.php:

PHP example
<?php

declare(strict_types=1);

require __DIR__ . '/helpers.php';

$product = ['name' => 'Notebook', 'price_cents' => 1299];

echo formatProductLine($product) . "\n";

// Prints:
// Notebook: 1299 cents

require loads the helper file before the function is called. If the file cannot be loaded, PHP stops with an error. That is usually what you want for required application code.

Use __DIR__ For Paths

Relative paths can break when a script is run from a different working directory. __DIR__ gives the directory of the current file.

PHP example
<?php

require __DIR__ . '/helpers.php';

This is more reliable than require 'helpers.php'; because it does not depend on where the command was run from.

require_once

Use require_once when loading the same file twice would cause problems, such as defining the same function twice.

PHP example
<?php

require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/helpers.php';

echo "Loaded once\n";

// Prints:
// Loaded once

Do not use includes to hide a messy design. If every file loads many other files in a surprising order, the project becomes hard to reason about.

Config Files That Return Arrays

A PHP file can return a value. This is common for simple configuration.

config.php:

PHP example
<?php

return [
    'currency' => 'GBP',
    'items_per_page' => 20,
];

report.php:

PHP example
<?php

$config = require __DIR__ . '/config.php';

echo $config['currency'] . "\n";

// Prints:
// GBP

This keeps configuration data separate from the code that uses it.

File Organization Rules

Put reusable functions in helper files. Put data or settings in config files. Put output and script flow in the main script.

Avoid helper files that print output as soon as they are required. Loading a file should normally define functions, return data, or bootstrap the application deliberately.

Common Mistakes

Do not build include paths from user input. That can become a file inclusion vulnerability.

Do not rely on the current working directory for important includes. Use __DIR__.

Do not include a file that has hidden side effects unless that is the point of the file and the name makes it obvious.

What You Should Be Able To Do

After this lesson, you should be able to:

  • use require to load a helper file;
  • use __DIR__ to build stable paths;
  • explain when require_once is useful;
  • return an array from a config file;
  • separate reusable helper code from script output.

Practice

Task: Load Helper File

Task

Show the contents of two files:

  • helpers.php, defining formatProductLine(array $product): string;
  • report.php, requiring helpers.php with __DIR__, creating one product, and printing the formatted line.
Show solution

Solution

helpers.php:

PHP example
<?php

declare(strict_types=1);

function formatProductLine(array $product): string
{
    return $product['name'] . ': ' . $product['price_cents'] . ' cents';
}

report.php:

PHP example
<?php

declare(strict_types=1);

require __DIR__ . '/helpers.php';

$product = ['name' => 'Notebook', 'price_cents' => 1299];

echo formatProductLine($product) . "\n";

// Prints:
// Notebook: 1299 cents

Explanation

The helper file defines the reusable function. The report file loads it, owns the data, and prints the result.

Task: Fix Require Path

Task

Fix this fragile include:

PHP example
<?php

require 'helpers.php';

Rewrite it so the path is based on the current file's directory.

Show solution

Solution

PHP example
<?php

require __DIR__ . '/helpers.php';

Explanation

__DIR__ points to the directory of the current file. That makes the include stable even when the script is run from a different working directory.

Task: Return Config Array

Task

Show two files:

  • config.php, returning an array with currency set to GBP and items_per_page set to 20;
  • report.php, requiring config.php and printing the currency.
Show solution

Solution

config.php:

PHP example
<?php

return [
    'currency' => 'GBP',
    'items_per_page' => 20,
];

report.php:

PHP example
<?php

$config = require __DIR__ . '/config.php';

echo $config['currency'] . "\n";

// Prints:
// GBP

Explanation

require returns the value returned by the loaded file. That makes small PHP config files simple to consume.