0
0
PHPprogramming~5 mins

Return values in PHP

Choose your learning style9 modes available
Introduction

Return values let a function send back a result after it finishes. This helps you reuse the result later in your program.

When you want a function to calculate and give back a number or text.
When you need to get a value from a function to use somewhere else.
When you want to check if a function worked and get a status.
When you want to keep your code organized by separating tasks into functions.
Syntax
PHP
<?php
function functionName() {
    // code
    return value;
}
?>

The return keyword sends a value back and stops the function.

After return, no code in the function runs.

Examples
This function adds two numbers and returns the sum.
PHP
<?php
function add($a, $b) {
    return $a + $b;
}
?>
This function returns a greeting string.
PHP
<?php
function greet() {
    return "Hello!";
}
?>
This function returns true if the number is even, otherwise false.
PHP
<?php
function isEven($num) {
    return $num % 2 === 0;
}
?>
Sample Program

This program defines a function to multiply two numbers and returns the result. Then it prints the result.

PHP
<?php
function multiply($x, $y) {
    return $x * $y;
}

$result = multiply(4, 5);
echo "4 times 5 is " . $result . "\n";
?>
OutputSuccess
Important Notes

Use return to send back any type of value: numbers, strings, booleans, or even arrays.

Once a function hits return, it stops running immediately.

Summary

Functions use return to send back results.

The returned value can be saved or used elsewhere.

Code after return inside a function does not run.