PHP Function Parameters
In this page:
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
// Define the function `greet` taking `$name`
function greet($name) {
// Print "Hello, $name!" to the output
echo "Hello, $name!";
}
// Call `greet("Alice")`
greet("Alice");
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि किसी parameter में एक change caller के variable को affect करेगा, जबकि arguments value से pass होते हैं जब तक
&से declare न किए जाएँ। - बहुत कम arguments pass करना, जो PHP 7.1 और बाद में एक
ArgumentCountErrorthrow करता है। intparameter को"abc"जैसा string pass करना, जोdeclare(strict_types=1)के तहत एकTypeErrorthrow करता है और अन्यथा coerce होता है या fail हो जाता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: