Functions in PHP
Create reusable blocks of code using PHP functions with parameters and return values.
Andrew Turner
September 29, 2025
2.6k94
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