Learn about functions in PHP, including how to define them, pass arguments, return values, and use default parameters. This guide includes clear examples for beginners.
<?php
function sayHello() {
echo "Hello, PHP!";
}
sayHello(); // Output: Hello, PHP!
?>
<?php
function greet($name) {
echo "Hello, $name!";
}
greet("Alice"); // Output: Hello, Alice!
?>
return
statement.<?php
function add($a, $b) {
return $a + $b;
}
echo add(5, 3); // Output: 8
?>
<?php
function setHeight($height = 150) {
echo "The height is: $height cm";
}
setHeight(); // Output: The height is: 150 cm
setHeight(180); // Output: The height is: 180 cm
?>