What are PHP functions?
In PHP, a function is a block of reusable code designed to perform a specific task. Instead of writing the same code multiple times, you can create a function once and call it whenever needed. This helps keep your code organized, reduces redundancy, and makes your programs easier to maintain.
There are two types of functions in PHP:
- Built-in functions β Predefined functions provided by PHP.
- User-defined functions β Custom functions created by programmers.
What are PHP functions used for?
Functions in PHP are used for various purposes, such as:
- Performing calculations.
- Handling strings and arrays.
- Processing user input.
- Connecting to databases.
- Manipulating files and directories.
- Generating dynamic web content.
Using functions makes your PHP scripts more structured and efficient.
Common built-in PHP functions
PHP provides a vast number of built-in functions that simplify coding. Here are some essential ones:
1. String functions
strlen($string)
: Returns the length of a string.strtoupper($string)
: Converts a string to uppercase.strtolower($string)
: Converts a string to lowercase.
Example:
$text = "Hello World!";
echo strlen($text); // Output: 12
echo strtoupper($text); // Output: HELLO WORLD!
π‘ PHP reading exercise – what does this code do?
$mytext = "Hello! This is Sander from Patchstack and I totally did not use ChatGPT for this particular article!";
if ( strlen($mytext) > 30 ) {
echo "I cannot focus on long texts, sry bro. Can you TLDR?";
}
else {
echo "What's up?";
}
2. Array functions
count($array)
: Counts the number of elements in an array.array_push($array, $value)
: Adds a value to an array.array_pop($array)
: Removes the last element from an array.
Example:
$fruits = ["Apple", "Banana"];
array_push($fruits, "Orange");
print_r($fruits); // Output: Array ( [0] => Apple [1] => Banana [2] => Orange )
3. Mathematical functions
round($number)
: Rounds a number.rand($min, $max)
: Generates a random number.
Example:
echo rand(1, 100); // Random number between 1 and 100
4. Date and time functions
date($format)
: Formats a date. (like Y-m-d H:i:s)time()
: Returns the current timestamp.
Example:
echo date("d.m.Y"); // Output: Current date and time
Creating a custom function in PHP
You can create your own functions in PHP using the function
keyword. Hereβs an example of a simple custom function:
function greetUser($name) {
echo "Hello, " . $name . "! Welcome to PHP.";
}
greetUser("Alice"); // Output: Hello, Alice! Welcome to PHP.
Breaking it down:
- The function
greetUser()
is defined with one parameter$name
. - The function prints a greeting message.
- It is called with the argument
"Alice"
, which outputs a personalized greeting.
Independent exercise
Create a custom PHP function named printMessage()
that prints any message you choose when it is called.
Bonus points: In the function, include today’s date. So you will need to echo the date() function inside your custom function.
Result should look like this:
Welcome to learning PHP functions!
Today's date is: 05.12.2025
Try writing this function in a PHP script and run it. Good luck! π