php

Create function in PHP

<?php
    function greeting($username) {
        echo "Hello : " . $username;
    }

    greeting("John");
?>
Output
Hello : John

To create a function in PHP simply add function keyword before function name and write code inside its code block. You can run a chunk of code using the PHP function and define as many functions as you want inside the PHP script.

You can also pass parameters to a function and can retrieve that parameter by its name inside the function.

Was this helpful?