PHP includes hundreds of ready-to-use functions. Here are three common string functions.
$str = "Hello, PHP World!";
strlen($str); // Counts characters
strtoupper($str); // Converts to uppercase
str_replace("PHP","INFOST 440",$str); // Replaces text
strlen() output: 17 characters
strtoupper() output: HELLO, PHP WORLD!
str_replace() output: Hello, INFOST 440 World!
These functions were written by the programmer to perform specific tasks.
function calculateGrade($score) {
if ($score >= 90) { return "A"; }
elseif ($score >= 80) { return "B"; }
elseif ($score >= 70) { return "C"; }
elseif ($score >= 60) { return "D"; }
else { return "F"; }
}
Score 95 → Grade: A
Score 83 → Grade: B
Score 74 → Grade: C
Score 61 → Grade: D
Score 45 → Grade: F
function formatCurrency($amount) {
return "$" . number_format($amount, 2);
}
1200 → $1,200.00
49.9 → $49.90
1000000 → $1,000,000.00
A function is a reusable block of code designed to perform a specific task.
Functions prevent code repetition and make scripts easier to read and maintain. PHP provides
two types. Built-in functions are included with PHP itself and can be called
directly without any setup — examples include strlen(), strtoupper(),
and array_push(). User-defined functions are created by the
programmer using the function keyword, a name, and parentheses that can hold
parameters as inputs. When called, PHP executes the code inside and can return a result back
to the caller. Parameters allow functions to work with different data each time they are called,
making them powerful and flexible building blocks for any PHP application.
INFOST 440 — Activity 3 — Nyla Broughton