What is Function Creation in PHP? Give Examples

Function creation is the process of defining your own custom functions using the function keyword. A function can take parameters, perform operations, and optionally return a value.


Basic Syntax

function functionName() {
    // code here
}

Example: Simple Function

function greetUser() {
    echo "Welcome to PHP!";
}

greetUser();

Function with Parameters

function greet($name) {
    echo "Hello, $name!";
}

greet("Sagar");

Function with Return Value

function add($a, $b) {
    return $a + $b;
}

echo add(10, 5);

Default Parameter Example

function welcome($name = "Guest") {
    echo "Welcome, $name!";
}

Conclusion

Function creation helps you write structured, readable, and efficient PHP programs—an essential skill for all PHP developers.


Citations

🔗 View other articles about PHP:
http://savanka.com/category/learn/php/

🔗 External PHP Documentation:
https://www.php.net/manual/en/

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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