Loading Depth Study
Preparing your workspace
Functions organize reusable logic, while type declarations make function contracts clearer and safer.
Functions allow repeated logic to be written once and reused. PHP supports parameters, default values, return statements, and typed parameters and returns. Modern PHP code benefits from clear function contracts because they make expectations easier to understand. Functions should normally have a focused responsibility and descriptive names. Strict typing can also be enabled to make type behavior more predictable.
A learning system can have a function that calculates course completion percentage from completed and total lessons.
<?php
declare(strict_types=1);
function calculateProgress(int $completed, int $total): int
{
if ($total === 0) {
return 0;
}
return (int) round(($completed / $total) * 100);
}
echo calculateProgress(8, 10);The script prints 80.