Functions in PHP
Create reusable blocks of code using PHP functions with parameters and return values.
Andrew Turner
Sep 27, 2025
15.5k573
Functions let you reuse code instead of repeating it.
Simple function
function greet() {
echo "Hello!";
}
greet();
With parameters
function greetUser($name) {
return "Hello $name";
}
echo greetUser("Daniel");
Default values
function greetUser($name = "Guest") {
return "Hello $name";
}
Type hints (modern PHP)
function add(int $a, int $b): int {
return $a + $b;
}
Next: Variable scope and global variables.
#PHP#Functions#Beginner