codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

15 / 56 topics
14Functions in PHP15User-Defined Functions16Anonymous Functions17Arrow Functions18Recursion in PHP
Tutorials/PHP/User-Defined Functions
🐘PHP

User-Defined Functions

Updated 2026-05-15
10 min read

User-Defined Functions

Introduction

In PHP, functions are blocks of code that perform a specific task and can be reused throughout your application. They help in organizing code, making it more readable and maintainable. User-defined functions allow you to encapsulate logic into reusable components, which is essential for building scalable and efficient applications.

Concept

A function in PHP is defined using the function keyword followed by the function name and parentheses. You can pass parameters to functions and return values from them. Here’s a basic structure of a function:

function functionName($parameter1, $parameter2) {
    // Function body
    return $result;
}

- **Function Name**: The name of the function should be descriptive and follow PHP naming conventions (start with a letter or underscore, followed by letters, numbers, or underscores).
- **Parameters**: These are inputs that you can pass to the function. They act like local variables within the function.
- **Return Statement**: This is used to send back a value from the function. If no return statement is present, the function returns `null` by default.

## Examples

### Example 1: Basic Function

Let's start with a simple example of a function that adds two numbers:

```php
<CodeBlock language="php">
{`function add($a, $b) {
    return $a + $b;
}

$result = add(5, 3);
echo "The sum is: " . $result; // Output: The sum is: 8`}
</CodeBlock>

In this example, the add function takes two parameters $a and $b, adds them together, and returns the result. We then call this function with arguments 5 and 3, store the returned value in $result, and print it.

Example 2: Function with Default Parameters

PHP allows you to define default values for function parameters. This means that if a parameter is not provided when the function is called, the default value will be used:

<CodeBlock language="php">
{`function greet($name = "Guest") {
    return "Hello, " . $name . "!";
}

echo greet(); // Output: Hello, Guest!
echo greet("Alice"); // Output: Hello, Alice!`}
</CodeBlock>

In this example, the greet function has a default parameter $name set to "Guest". If no argument is passed when calling the function, it will use the default value.

Example 3: Function with Multiple Return Points

A function can have multiple return points. The execution of the function stops as soon as a return statement is encountered:

<CodeBlock language="php">
{`function checkAge($age) {
    if ($age < 18) {
        return "You are a minor.";
    } else {
        return "You are an adult.";
    }
}

echo checkAge(15); // Output: You are a minor.
echo checkAge(20); // Output: You are an adult.`}
</CodeBlock>

In this example, the checkAge function checks if the provided age is less than 18 and returns a corresponding message. The function has two return points based on the condition.

Example 4: Returning Arrays

Functions can also return arrays, which can be useful for returning multiple values:

<CodeBlock language="php">
{`function getUserInfo($name, $age) {
    return array("name" => $name, "age" => $age);
}

$user = getUserInfo("Bob", 30);
echo "Name: " . $user["name"] . ", Age: " . $user["age"]; // Output: Name: Bob, Age: 30`}
</CodeBlock>

In this example, the getUserInfo function returns an associative array containing the user's name and age. We then access these values using their keys.

What's Next?

Now that you have a good understanding of defining custom functions in PHP, you might want to explore more advanced topics such as Anonymous Functions. These are functions without a specified name and can be defined inline or passed around as variables. They provide flexibility and are often used in functional programming paradigms.

Stay tuned for the next section where we dive deeper into anonymous functions and their use cases!


PreviousFunctions in PHPNext Anonymous Functions

Recommended Gear

Functions in PHPAnonymous Functions