PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP return Keyword: Function Return Value

The return keyword in PHP is used to stop the execution of a function and send a value back to the part of the program that called the function. This returned value can then be used in the rest of your program.

Here's a simple example of a function that returns a value:

function sum($a, $b) {
    $result = $a + $b;
    return $result;
}

$total = sum(5, 10);
echo $total; // Outputs: 15

In this example, the sum function takes two parameters, adds them together, and then returns the result. When the function is called with the arguments 5 and 10, it returns 15, which is then assigned to the variable $total.

A few important things to note about the return keyword in PHP:

  1. Ending Execution: As soon as PHP encounters a return statement, it will immediately end execution of the current function and return control to the line that called the function. Any code that follows the return statement in the function will be ignored.

  2. Returning Multiple Values: PHP functions can only return one value. If you want to return multiple values, you can return an array or an object that contains the values.

  3. Returning by Reference: By default, the return statement returns a value, not a variable. If you want to return a variable by reference, you can use the & symbol in the function declaration. However, returning by reference is not commonly used and can lead to confusing code if not handled carefully.

  4. Without a Value: If a function doesn't include a return statement, it will return NULL by default. The return keyword can also be used on its own to end execution of a function without returning a value.

function sayHello($name) {
    if (empty($name)) {
        return; // Ends execution if $name is empty
    }
    echo "Hello, $name!";
}

sayHello(""); // Outputs nothing
  1. How to use return in PHP functions:

    • Basic usage of the return keyword in a PHP function:
    <?php
    function add($a, $b) {
        return $a + $b;
    }
    
    // Call the function and use the returned value
    $result = add(3, 5);
    echo $result; // Output: 8
    
  2. Returning values from PHP functions:

    • Functions can return values using the return keyword:
    <?php
    function square($number) {
        return $number * $number;
    }
    
    // Call the function and use the returned value
    $result = square(4);
    echo $result; // Output: 16
    
  3. Void vs. non-void return in PHP:

    • Functions without a return statement or with return; are considered void:
    <?php
    function printMessage() {
        echo "Hello, World!";
        // No return statement or return; (void)
    }
    
    printMessage(); // Output: Hello, World!
    
  4. Multiple return statements in PHP functions:

    • Functions can have multiple return statements based on conditions:
    <?php
    function absoluteValue($number) {
        if ($number < 0) {
            return -$number;
        } else {
            return $number;
        }
    }
    
    // Call the function with different arguments
    echo absoluteValue(-5); // Output: 5
    echo absoluteValue(3);  // Output: 3
    
  5. Early return in PHP functions:

    • Use early return to exit the function prematurely:
    <?php
    function checkEven($number) {
        if ($number % 2 !== 0) {
            return false;
        }
        return true;
    }
    
    // Call the function with different arguments
    var_dump(checkEven(4)); // Output: bool(true)
    var_dump(checkEven(3)); // Output: bool(false)
    
  6. Returning arrays and objects in PHP:

    • Functions can return arrays, objects, or any other data type:
    <?php
    function getPerson() {
        return ['name' => 'John', 'age' => 30];
    }
    
    // Call the function and use the returned array
    $person = getPerson();
    echo $person['name']; // Output: John
    
  7. Error handling with return in PHP:

    • Use return to handle errors or exceptional cases in functions:
    <?php
    function divide($a, $b) {
        if ($b === 0) {
            return 'Error: Division by zero';
        }
        return $a / $b;
    }
    
    // Call the function and handle the returned value
    $result = divide(10, 0);
    echo $result; // Output: Error: Division by zero
    
  8. Conditional returns in PHP functions:

    • Return different values based on conditions:
    <?php
    function checkNumber($number) {
        return ($number > 0) ? 'Positive' : 'Non-positive';
    }
    
    // Call the function with different arguments
    echo checkNumber(5);  // Output: Positive
    echo checkNumber(-3); // Output: Non-positive