PHP Return Values
In this page:
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
कई Values Return करना
एक PHP function सीधे सिर्फ एक value return कर सकता है, लेकिन आप कई related values को एक array में bundle करके इसके बजाय वह return कर सकते हैं — फिर call site पर list() या square-bracket destructuring से array को वापस अलग variables में unpack कर सकते हैं।
उदाहरण: Returning Multiple Values
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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));
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
returnके बाद code लिखना, जो कभी नहीं चलता क्योंकिreturnfunction को तुरंत खत्म कर देता है।returnभूल जाना और result इस्तेमाल करना, ताकि caller कोnullमिले।: intdeclare करना और"abc"जैसी एक string return करना, जो एकTypeErrorthrow करता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: