php language basics
Programming Paradigms in PHP
At beginner level, the practical goal is simple: choose a shape that makes the next change easy to understand. You do not need to turn every small script into classes, and you should not leave every growing application as one long procedural file.
Procedural Script With A Helper
<?php
declare(strict_types=1);
function total_cents(array $items): int
{
$total = 0;
foreach ($items as $item) {
$total += $item['price_cents'];
}
return $total;
}
$basket = [
['name' => 'Notebook', 'price_cents' => 1299],
['name' => 'Pen', 'price_cents' => 199],
];
echo total_cents($basket) . "\n";
// Prints:
// 1498
This example uses a procedural script with a small function: the script prepares the basket, and the function calculates the total. That is enough to introduce the idea of programming style without pretending that every lesson needs classes, frameworks, or architecture language.
When Procedural Code Is Fine
Procedural code is code that runs step by step. It is fine for small scripts, one-off reports, simple command-line tools, and early learning examples.
The warning sign is not "procedural code exists." The warning sign is a long file where validation, calculation, output, database calls, and configuration are mixed together with no named boundaries.
Functional-Style Helpers
A function that accepts data and returns a value is easy to test and reuse.
<?php
declare(strict_types=1);
function productNames(array $products): array
{
return array_map(
fn (array $product): string => $product['name'],
$products
);
}
$products = [
['name' => 'Notebook'],
['name' => 'Pen'],
];
print_r(productNames($products));
// Prints:
// Array
// (
// [0] => Notebook
// [1] => Pen
// )
This style works well for transformations: take input, return output, avoid hidden state.
Object-Oriented Shape
Object-oriented code groups data and behavior into objects. Full OOP comes later, but the basic idea is visible in a small class.
<?php
declare(strict_types=1);
final class Basket
{
public function __construct(private array $items)
{
}
public function totalCents(): int
{
$total = 0;
foreach ($this->items as $item) {
$total += $item['price_cents'];
}
return $total;
}
}
$basket = new Basket([
['name' => 'Notebook', 'price_cents' => 1299],
['name' => 'Pen', 'price_cents' => 199],
]);
echo $basket->totalCents() . "\n";
// Prints:
// 1498
This is heavier than a function, but it starts to make sense when behavior and data travel together or when the code grows into multiple related operations.
Choosing A Style
Use the simplest style that keeps the rule clear. A one-file script can use procedural code plus helpers. A repeated calculation can become a function. A concept with state and several related behaviors may become a class later.
Do not rewrite code into a new paradigm just to look more advanced. A junior developer should be able to explain why the chosen shape helps.
What You Should Be Able To Do
After this lesson, you should be able to:
- recognize procedural, function-based, and object-oriented shapes;
- choose a simple function when code is repeated;
- avoid turning every small script into classes too early;
- explain when a class might be useful;
- refactor a repeated calculation into a named helper.
Practice
Task: Choose A Shape
Task
For each situation, choose procedural script, function, or class, and write one sentence explaining why.
- A one-off CLI script prints three product names.
- A price formatting rule is repeated in five places.
- A basket will soon need totals, discounts, shipping, and tax behavior.
Show solution
Solution
- A one-off CLI script prints three product names: use a procedural script because there is not yet repeated behavior to name.
- A price formatting rule is repeated in five places: use a function because one named helper removes duplication.
- A basket will soon need totals, discounts, shipping, and tax behavior: consider a class because related state and behavior are starting to travel together.
Explanation
The point is not to pick the most advanced shape. The point is to choose the smallest shape that keeps the next change clear.
Task: Pick Clear Style
Task
Read this requirement:
Print a short inventory report from a hard-coded array of products.
Choose whether you would start with a procedural script, a function, or a class. Write a short answer explaining the choice and when you would change the shape later.
Show solution
Solution
Start with a procedural script plus one helper function if formatting is repeated. The data is hard-coded and the behavior is small, so a class would add structure before there is a real design pressure.
Change the shape later if the report starts loading data from several sources, applying several rules, or sharing behavior with other scripts.
Explanation
This is a pragmatic choice. The code should be easy to read today and still have a clean path to refactor later.
Task: Refactor To Function
Task
Refactor this repeated calculation into a function named totalCents(array $items): int.
<?php
$items = [
['name' => 'Notebook', 'price_cents' => 1299],
['name' => 'Pen', 'price_cents' => 199],
];
$total = 0;
foreach ($items as $item) {
$total += $item['price_cents'];
}
echo $total . "\n";
Show solution
Solution
<?php
declare(strict_types=1);
function totalCents(array $items): int
{
$total = 0;
foreach ($items as $item) {
$total += $item['price_cents'];
}
return $total;
}
$items = [
['name' => 'Notebook', 'price_cents' => 1299],
['name' => 'Pen', 'price_cents' => 199],
];
echo totalCents($items) . "\n";
// Prints:
// 1498
Explanation
The calculation now has a name and can be reused. The script still owns the data and output, which is enough structure for this small example.