← Back to PHP Course | Chapter 5: Functions | Lesson 3 of 10

PHP Return Values

एक return value वह answer है जो function वापस सौंपता है, किसी vending machine के आपके pay करने के बाद snack देने जैसा। Function answer देते ही रुक जाता है।
Syntax
php
function function_name($parameter): return_type {
    return $value;
}

$result = function_name($argument);

Values Return करना

return जिस code ने function call किया उसे एक value वापस भेजता है, और return चलते ही function का execution रुक जाता है — उसी function के अंदर इसके बाद लिखा कोई भी code कभी execute नहीं होता।

उदाहरण: Returning Values

php
<?php
// Define the function `add` taking `$a`, `$b`
function add($a, $b) {
    // Return `$a + $b` from this function
    return $a + $b;
    // Print "This never runs" to the output
    echo "This never runs";
}
// Print `add(2, 3)` to the output
echo add(2, 3);
?>

कई Values Return करना

एक PHP function सीधे सिर्फ एक value return कर सकता है, लेकिन आप कई related values को एक array में bundle करके इसके बजाय वह return कर सकते हैं — फिर call site पर list() या square-bracket destructuring से array को वापस अलग variables में unpack कर सकते हैं।

उदाहरण: Returning Multiple Values

php
<?php
// Define the function `getName` with no parameters
function getName() {
    // Return `["Alice", "Smith"]` from this function
    return ["Alice", "Smith"];
}
[$first, $last] = getName();
// Print "$first $last" to the output
echo "$first $last";
?>

Return Type Declarations

किसी function के closing parenthesis के बाद : type add करना declare करता है कि उसकी return value का type क्या होना चाहिए।

अगर function की body कुछ ऐसा return करने की कोशिश करे जो match न करे, PHP एक TypeError raise करता है — यह bugs की एक पूरी class को source पर ही पकड़ लेता है बजाय जहाँ भी mismatched value आख़िरकार problem create करे।

उदाहरण: Return Type Declarations

php
<?php
// Define the function `add` taking `int $a`, `int $b`
function add(int $a, int $b): int {
    // Return `$a + $b` from this function
    return $a + $b;
}
// Print `add(2, 3)` to the output
echo add(2, 3);
?>

Nullable Return Types

किसी return type से पहले ? prefix करना (function find(): ?User) इसे nullable mark करता है, explicitly function को declared type या null दोनों में से कोई एक return करने की अनुमति देते हुए।

उस ? के बिना, User return करने के लिए typed किसी function से null return करना एक type error trigger करेगा।

उदाहरण: Nullable Return Types

php
<?php
// Define the function `findUser` taking `bool $exists`
function findUser(bool $exists): ?string {
    // Return `$exists ? "Alice" : null` from this function
    return $exists ? "Alice" : null;
}
// Print a detailed dump (with types) of `findUser(false)`
var_dump(findUser(false));
?>

Void Return Type

एक void return type PHP का यह बताने का तरीका है कि एक function एक action perform करता है लेकिन जानबूझकर वापस सौंपने के लिए कोई value produce नहीं करता — मान लीजिए, एक message log करना, या एक database row update करना।

एक void function के अंदर से एक actual expression return करने की कोशिश करना एक compile-time error है, सिर्फ एक style violation नहीं।

उदाहरण: Void Return Type

php
<?php
// Define the function `logMessage` taking `string $msg`
function logMessage(string $msg): void {
    // Print "LOG: $msg" to the output
    echo "LOG: $msg";
}
// Call `logMessage("Saved successfully")`
logMessage("Saved successfully");
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. return के बाद code लिखना, जो कभी नहीं चलता क्योंकि return function को तुरंत खत्म कर देता है।
  2. return भूल जाना और result इस्तेमाल करना, ताकि caller को null मिले।
  3. : int declare करना और "abc" जैसी एक string return करना, जो एक TypeError throw करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.