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

PHP Function Parameters

Parameters किसी recipe पर blanks जैसे हैं जिन्हें आप हर बार भरते हैं, जैसे आप कितने लोगों के लिए cook कर रहे हैं। Function जब आप इसे call करते हैं तब आप जो भी दें उसे इस्तेमाल करता है।
Syntax
php
function function_name($parameter1, $parameter2) {
    // body
}

function function_name(&$by_reference) { }   // pass by reference

function_name($argument1, $argument2);

Parameters क्या हैं?

Parameters वे named placeholders हैं जो किसी function को define करते समय उसके parentheses के अंदर list किए जाते हैं।

जब आप बाद में function *call* करते हैं, आपके दिए गए arguments उन placeholders को order में भर देते हैं, function की body को काम करने के लिए कुछ concrete देते हुए।

उदाहरण: What are Parameters?

php
<?php
// Define the function `greet` taking `$name`
function greet($name) {
    // Print "Hello, $name!" to the output
    echo "Hello, $name!";
}
// Call `greet("Alice")`
greet("Alice");
?>

Pass by Value

Default रूप से, PHP किसी argument की value को function में copy कर देता है — function body के अंदर parameter में किए गए किसी भी change local रहते हैं और वहाँ मौजूद original variable को कभी नहीं छूते जहाँ function call हुआ था।

यही वजह है कि plain function calls गलती से आपके pass किए किसी variable को mutate नहीं कर सकते।

उदाहरण: Pass by Value

php
<?php
// Define the function `addOne` taking `$num`
function addOne($num) {
    // Declare `$num`, set to `$num + 1`
    $num = $num + 1;
    // Print "Inside: $num\n" to the output
    echo "Inside: $num\n";
}
// Declare `$x`, set to `5`
$x = 5;
// Call `addOne($x)`
addOne($x);
// Print "Outside: $x" to the output
echo "Outside: $x";
?>

Pass by Reference

Function definition में किसी parameter name से पहले & prefix करना इसे pass-by-reference में switch कर देता है: function अब असली variable खुद पर operate करता है, इसलिए इसके किए किसी भी change function के return होते ही call site पर वापस visible है — default copy behavior का उल्टा।

उदाहरण: Pass by Reference

php
<?php
// Define the function `addOne` taking `&$num`
function addOne(&$num) {
    // Declare `$num`, set to `$num + 1`
    $num = $num + 1;
}
// Declare `$x`, set to `5`
$x = 5;
// Call `addOne($x)`
addOne($x);
// Print `$x` to the output
echo $x;
?>

Typed Parameters

किसी parameter name से पहले एक type declaration add करना (function greet(string $name)) PHP को call site पर उस type को enforce करने को बताता है।

कोई ऐसी चीज़ pass करें जो match न करे — एक array जहाँ एक string expected थी — और PHP तुरंत एक TypeError throw करता है, बाद में चुपचाप coerce करने या misbehave करने के बजाय।

उदाहरण: Typed Parameters

php
<?php
// Define the function `greet` taking `string $name`
function greet(string $name) {
    // Print "Hello, $name!" to the output
    echo "Hello, $name!";
}
// Call `greet("Alice")`
greet("Alice");
?>

Variadic Parameters (... operator)

किसी parameter name से पहले ... operator function के अंदर किसी भी संख्या के extra arguments को एक single array में collect कर देता है, आपको एक ऐसा function लिखने देते हुए जो एक flexible, unknown-in-advance संख्या में values accept करे बजाय एक fixed parameter list के।

उदाहरण: Variadic Parameters (... operator)

php
<?php
// Define the function `sumAll` taking `...$numbers`
function sumAll(...$numbers) {
    // Return `array_sum($numbers)` from this function
    return array_sum($numbers);
}
// Print `sumAll(1, 2, 3, 4)` to the output
echo sumAll(1, 2, 3, 4);
?>
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. यह उम्मीद करना कि किसी parameter में एक change caller के variable को affect करेगा, जबकि arguments value से pass होते हैं जब तक & से declare न किए जाएँ।
  2. बहुत कम arguments pass करना, जो PHP 7.1 और बाद में एक ArgumentCountError throw करता है।
  3. int parameter को "abc" जैसा string pass करना, जो declare(strict_types=1) के तहत एक TypeError throw करता है और अन्यथा coerce होता है या fail हो जाता है।

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.