How to create a function in PHP?

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 named greet with a parameter $name.
  • return sends the result back to the caller.
  • echo greet("Alice") calls the function with "Alice" as the argument.

Leave a Reply

Your email address will not be published. Required fields are marked *