Functions
A function is a named piece of a PHP program that runs whenever its name is called. It keeps one idea in one place and gives that place a name the rest of the code can use.
Define and call
A function wraps a named block of logic so it can run many times from many places. The function keyword names the block, and parentheses after the name run it. Define once at the top level, then call by name wherever the result is needed.
<?php function greet() { echo "hello", PHP_EOL; } greet();
php define.php
hello
The definition alone prints nothing; only the call runs the body. The name is case-insensitive, so greet() and Greet() reach the same function.
Arguments and defaults
Parameters listed in the definition receive the arguments passed at the call site. A default value makes a parameter optional, and a type declaration rejects wrong input early with a clear error instead of a strange result deep inside the body.
<?php function greet(string $name = "guest"): void { echo "hello " . $name, PHP_EOL; } greet("Ada"); greet();
php args.php
hello Ada
hello guest
The first call passes its argument and the second one leaves the parameter out, so the default "guest" fills it. The void return type says that this function is called for its echo and has no value to give back.
Return values
The return statement sends a value back to the caller and ends the function at once. A function without return gives back null. Early returns for edge cases keep the main path flat and easy to follow.
<?php function add(int $a, int $b): int { return $a + $b; } function note(): void { echo "noted", PHP_EOL; } echo add(2, 3), PHP_EOL; var_dump(note());
php return.php
5
noted
NULL
The first call returns the sum and echo prints it. The second function prints its line and returns nothing, which the dump shows as NULL: that is what a call to a function without a value gives back.
Reuse across files
Shared helpers live in their own file and load once with require_once, so every page uses the same tested code. See Call function from another file for the full walkthrough of defining in one file and calling from another.
The file helpers.php:
<?php function slug(string $text): string { return strtolower(str_replace(" ", "-", $text)); }
The page that uses it, index.php:
<?php require_once(__DIR__ . "/helpers.php"); echo slug("Hello World"), PHP_EOL;
php index.php
hello-world
The helper leans on str_replace() to swap the space for a dash and strtolower() to lower the letters. The require_once loads the file once, so a second include of the same path does not define slug() twice.
Exercises
- Add a parameter to greet() in define.php and print it. What happens if the call passes nothing?
- Give the name parameter in args.php a type of int and call greet("Ada"). What does PHP say?
- Write a function that returns the larger of two numbers, then print it for the pairs (3, 7) and (7, 3).
- Add a second helper to helpers.php that counts the words of a slug, require it in index.php and print both results.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the difference between a parameter and an argument?
Answer
The parameter is declared in the definition, the argument is passed at the call
The definition lists parameters with their types and defaults; the call supplies the arguments that fill them.
-
What does a default value in a parameter list do?
Answer
It makes the parameter optional
A parameter with a default may be left out of the call, and the default then fills it.
-
What does a function without a
returnstatement give back?Answer
nullA call with no return value evaluates to null, as var_dump of such a call shows.
-
What does a type declaration on a parameter do?
Answer
It rejects an argument of the wrong type with a TypeError
The type is checked when the function is called, so a wrong argument stops the call with a clear error.
-
What does this program print?
<?php function double(int $n): int { return $n * 2; } echo double(21);Answer
42
The function returns twice its argument, and 21 doubled is 42.
-
What does this program print?
<?php function greet(string $name = "guest") { return "hi " . $name; } echo greet();Answer
hi guest
The call passes no argument, so the default "guest" fills the parameter.
-
What does this program print?
<?php function add(int $a, int $b) { echo $a + $b; } add(2, 3);Answer
5
The function prints the sum itself, so the call shows 5 and returns nothing worth printing.
-
What does this program print?
<?php function note() { echo "noted"; } var_dump(note());Answer
notedNULL
The echo prints noted without a line break, and the dump of the returned null prints NULL right after it.
-
What does this program print?
<?php function count_words(string $text): int { return count(explode(" ", $text)); } echo count_words("one two three");Answer
3
The string holds three words, explode cuts it at the spaces and count reports three pieces.
-
What does
require_oncedo when the same file is included a second time?Answer
It skips the second include, so nothing is defined twice
The once part remembers the path, so a repeated include of the same file does nothing.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev