Functions in PHP: Define, Call & Return

    Atul Kabra3 min readUpdated

    Functions in PHP

    A function is a reusable block of code that does one job. You define it once and call it whenever you need it, which keeps your code organised and avoids repetition. PHP has thousands of built-in functions and lets you write your own.

    Defining and calling a function

    Use the function keyword, give it a name, and put the code inside curly braces.

    <?php
    // Define a function.
    function greet() {
        echo "Welcome to PHP!";
    }
    
    // Call (run) the function.
    greet(); // prints: Welcome to PHP!
    ?>
    

    Passing parameters

    Parameters let you send data into a function so it can work with different values.

    <?php
    // $name is a parameter.
    function greetUser($name) {
        echo "Hello, $name!";
    }
    
    greetUser("Meera"); // Hello, Meera!
    greetUser("Arjun"); // Hello, Arjun!
    ?>
    

    Returning values

    A function can send a result back using return. The caller can then store or use that value.

    <?php
    function addNumbers($a, $b) {
        return $a + $b; // send the result back
    }
    
    $sum = addNumbers(7, 3);
    echo $sum; // 10
    ?>
    

    Once return runs, the function stops immediately.

    Default parameter values

    You can give a parameter a default so it is optional.

    <?php
    function greet($name = "Guest") {
        echo "Hi, $name";
    }
    
    greet();         // Hi, Guest  (uses the default)
    greet("Sahil");  // Hi, Sahil
    ?>
    

    Named arguments (PHP 8)

    PHP 8 lets you pass arguments by name, which makes calls clearer and lets you skip optional parameters.

    <?php
    function createUser($name, $role = "student", $active = true) {
        echo "$name | $role | " . ($active ? "active" : "inactive");
    }
    
    // Pass by name; order does not matter.
    createUser(name: "Tara", active: false);
    // Output: Tara | student | inactive
    ?>
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Type declarations

    You can declare the types of parameters and the return value. This helps catch mistakes early.

    <?php
    // Expect two integers, return an integer.
    function multiply(int $a, int $b): int {
        return $a * $b;
    }
    
    echo multiply(4, 5); // 20
    ?>
    

    Variable scope

    A variable created inside a function exists only inside that function. This is called local scope.

    <?php
    function showTotal() {
        $total = 100; // local to this function
        echo $total;
    }
    
    showTotal(); // 100
    // echo $total; // Error: $total is not defined out here
    ?>
    

    To use a value from outside, pass it in as a parameter rather than relying on global variables.

    Arrow functions and anonymous functions

    PHP also supports short functions you can assign to variables, useful with array functions.

    <?php
    $numbers = [1, 2, 3, 4];
    
    // Arrow function: double each number.
    $doubled = array_map(fn($n) => $n * 2, $numbers);
    
    print_r($doubled); // [2, 4, 6, 8]
    ?>
    

    Common mistakes

    • Calling a function before defining it in some contexts, or misspelling the function name.
    • Forgetting to return a value, so the caller receives null.
    • Expecting a local variable to be visible outside the function.
    • Mixing up parameters (in the definition) and arguments (in the call). Parameters are the placeholders; arguments are the actual values you pass.

    FAQ

    What is the difference between a parameter and an argument? A parameter is the variable named in the function definition. An argument is the real value you pass when calling the function.

    Can a function return more than one value? Not directly, but you can return an array (or an object) containing several values.

    What are named arguments good for? They make calls readable and let you skip optional parameters without passing them in order. They were added in PHP 8.

    Keep learning

    Back to the PHP & MySQL hub, revisit Arrays in PHP, or continue to Strings in PHP.

    Build a library of your own functions with mentor support. Join the waitlist for the PHP course at Infoplanet in Jalgaon.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides