PHP Function With Return Value: PHP में return का उपयोग किसी function से value को वापस (return) करने के लिए किया जाता है। जब कोई function किसी value को return करता है, तो हम उस value को आगे के उपयोग (जैसे calculation या output) के लिए store या उपयोग कर सकते हैं।
PHP Function with Return Value का Syntax:
<?php
function functionName($parameter1, $parameter2, ...) {
// कोड या प्रक्रिया
return $value; // फंक्शन से वैल्यू वापस करना
}
?>PHP- functionName: फंक्शन का नाम।
- $parameter1, $parameter2: फंक्शन के input arguments.
- return $value: फंक्शन से लौटाई गई (return की गई) value.
Example: Return Value के साथ Simple Function
<?php
function addNumbers($a, $b) {
return $a + $b; // दो नंबर का जोड़ return करें
}
$result = addNumbers(10, 20); // फंक्शन को कॉल करें और result में स्टोर करें
echo "Sum: $result";
// Output: Sum: 30
?>PHPPHP Function : Return Type Declaration
PHP में Return Type Declaration का उपयोग यह निर्धारित करने के लिए किया जाता है कि किसी फ़ंक्शन से किस प्रकार का डेटा वापस आएगा। यह PHP 7.0 में पेश किया गया था और कोड को अधिक सटीक और त्रुटिहीन बनाने में मदद करता है।
PHP में Return Type Declaration समझें:
जब आप किसी फ़ंक्शन की घोषणा करते हैं, तो आप फ़ंक्शन के बाद कॉलन : का उपयोग करके यह निर्दिष्ट कर सकते हैं कि वह फ़ंक्शन किस प्रकार का डेटा लौटाएगा।
Return Type Declaration Syntax:
<?php
function functionName(parameters): returnType {
// Function body
return value;
}
?>PHPExample:
<?php
function addNumbers(int $a, int $b): int {
return $a + $b;
}
echo addNumbers(10, 20);
// Output: 30
?>PHPयहाँ : int बताता है कि यह फ़ंक्शन एक integer (पूर्णांक) वापस करेगा।
Strict Mode In Php Function:
अगर हम define किये गए type की value return नहीं करते हैं तो वह function कुछ भी return नहीं करता है , या हो सकता है कि Warning generate करे और अगर Strict Mode On
हुई तो PHP Fatal Error Generate कर सकती है।
<?php
declare(strict_types=1);
function test1() : int
{
return "Hello Vikas";
}
echo Test1();
?>PHPPHP Fatal error: Uncaught TypeError: Return value of test1() must be of the type integer, string returned in C:\xampp\htdocs\test\fuction.php:5
Explain : Example में function का return type integer define किया गया है जबकि function string value return कर रहा है , इसलिए fatal error generate हुई।
Void Return Type:
यदि आप चाहते हैं कि फ़ंक्शन कुछ भी न लौटाए, तो void उपयोग करें।
<?php
function displayMessage(): void {
echo "This is a message.";
}
displayMessage(); // Output: This is a message.
?>PHPAdvantages of Return Value in Functions:
- Reusability: Function से return की गई value को कई बार उपयोग किया जा सकता है।
- Flexibility: Function को अलग-अलग scenarios में उपयोग किया जा सकता है।
- Simplified Logic: Complex operations को function के अंदर manage किया जा सकता है।
- Modularity: Functions को एक specific काम तक सीमित रखा जा सकता है।
Conclusion:
PHP में functions का उपयोग return values के साथ कोड को reusable और modular बनाने के लिए किया जाता है। Return value को calculations, conditions, और अन्य उपयोगों के लिए store और process किया जा सकता है।

Welcome to HindiTutorials, your go-to platform for learning and mastering concepts in Hindi! Our mission is to make education accessible and simple for everyone by providing high-quality tutorials on programming, technology, and various other topics.