Introduction:
Functions in PHP encapsulate reusable code. You define them using the function
keyword and call them by name.
Example:
<?php
function greet($name) {
return "Hello, " . $name . "!";
}
echo greet("Alice");
?>
Explanation:
function greet($name)
defines a function namedgreet
with a parameter$name
.return
sends the result back to the caller.echo greet("Alice")
calls the function with"Alice"
as the argument.