php language basics

Milestone: Data and Files

This milestone checks whether you can combine arrays, strings, and typed helper functions before the course moves into includes and multi-file scripts.

The important step is the boundary between "data" and "helper code." A small PHP script often starts with data in one file and helper functions in another. That split only helps if the helper has a clear input and return value.

Product Rows And A Formatter

PHP example
<?php

declare(strict_types=1);

function formatProductLine(array $product): string
{
    $name = trim((string) ($product['name'] ?? ''));
    $priceCents = (int) ($product['price_cents'] ?? 0);

    if ($name === '') {
        throw new InvalidArgumentException('Product name is required.');
    }

    if ($priceCents <= 0) {
        throw new InvalidArgumentException('Product price must be positive.');
    }

    return $name . ': ' . $priceCents . ' cents';
}

$products = [
    ['name' => 'Notebook', 'price_cents' => 1299],
    ['name' => 'Pen', 'price_cents' => 199],
];

foreach ($products as $product) {
    echo formatProductLine($product) . "\n";
}

// Prints:
// Notebook: 1299 cents
// Pen: 199 cents

The helper function receives one product row and returns one formatted string. The loop decides where to send the output. That separation makes the formatter easy to move into another file later.

What A File Boundary Changes

When code moves into a helper file, hidden dependencies become more painful. A helper should not depend on a random variable from the script that includes it. Pass values in as parameters and return values out.

The next includes lesson shows the mechanics. This milestone checks the design habit first: a helper file should contain reusable functions, not surprise output.

What This Milestone Proves

You are ready to move on when you can:

  • read a list of array records;
  • format each record with a typed helper function;
  • validate missing or invalid array keys;
  • return strings instead of printing inside the helper;
  • explain what should live in a helper file and what should stay in the main script.

Practice

Task: Format Products From Helper

Task

Write a function named formatProductLine(array $product): string.

It should return Notebook: 1299 cents for this product:

PHP example
<?php

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

Print the returned string outside the function.

Show solution

Solution

PHP example
<?php

declare(strict_types=1);

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

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

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

// Prints:
// Notebook: 1299 cents

Explanation

The helper returns the formatted string. The main script prints it. That separation is what makes the helper easy to move into a separate file.

Task: Fix Helper Return

Task

Fix this helper so it returns a string instead of printing inside the function:

PHP example
<?php

function productLine(array $product): void
{
    echo $product['name'] . ': ' . $product['price_cents'] . " cents\n";
}

The fixed helper should have return type string.

Show solution

Solution

PHP example
<?php

declare(strict_types=1);

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

$product = ['name' => 'Pen', 'price_cents' => 199];

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

// Prints:
// Pen: 199 cents

Explanation

Returning the string makes the helper reusable. The caller can print it, test it, write it to a file, or include it in a larger report.

Task: Build Two File Summary

Task

Describe the shape of a two-file product summary script.

Your answer should include:

  • helpers.php, containing formatProductLine(array $product): string;
  • report.php, requiring helpers.php, defining a product array, and printing the formatted line;
  • one sentence explaining why the helper should return instead of echo.
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 reusable behavior. The report file owns the data and output. Returning from the helper keeps that boundary clean.