PHP Functions Introduction
In this page:
function function_name() {
// body
}
function_name(); // call it
Functions Define और Call करना
एक function statements के एक block को एक नाम के तहत package करता है ताकि आप उसी logic को बिना दोबारा type किए बार-बार चला सकें।
PHP file load होने पर definition parse करता है, लेकिन अंदर का code actually सिर्फ तभी चलता है जब कोई इसे नाम से call करे।
उदाहरण: Defining and Calling Functions
<?php
// Define the function `greet` with no parameters
function greet() {
// Print "Hello!" to the output
echo "Hello!";
}
// Call `greet()`
greet();
?>
Login to try C/C++/Java/PHP code in the editor
Function Naming Rules
Function names को एक letter या underscore से शुरू होना चाहिए, और जबकि PHP खुद आपके इस्तेमाल किए casing की परवाह नहीं करता, consistent रहना — पूरे project में camelCase या snake_case — किसी codebase को scan और predict करना कहीं आसान बनाता है।
उदाहरण: Function Naming Rules
<?php
// Define the function `calculateTotal` with no parameters
function calculateTotal() {
// Return `100` from this function
return 100;
}
// Print `calculateTotal()` to the output
echo calculateTotal();
?>
Login to try C/C++/Java/PHP code in the editor
Functions के अंदर Variable Scope
किसी function के अंदर बनाया गया कोई भी variable सिर्फ उस function के खुद के execution के लिए exist करता है और return होते ही गायब हो जाता है — बाहर का code इसे नहीं देख सकता।
किसी function के अंदर आसपास की global scope से एक variable पढ़ने या modify करने के लिए, आपको पहले इसे explicitly global keyword से declare करना होगा।
उदाहरण: Variable Scope inside Functions
<?php
// Declare `$count`, set to `10`
$count = 10;
// Define the function `showCount` with no parameters
function showCount() {
global $count;
// Print `$count` to the output
echo $count;
}
// Call `showCount()`
showCount();
?>
Login to try C/C++/Java/PHP code in the editor
बिना Arguments वाले Functions
किसी function को उपयोगी होने के लिए parameters की ज़रूरत नहीं — बहुत सारे functions हर बार call होने पर बस एक fixed task perform करते हैं या एक fixed message print करते हैं, अपना काम करने के लिए बाहर से कुछ भी नहीं चाहिए।
उदाहरण: Functions with No Arguments
<?php
// Define the function `printWelcome` with no parameters
function printWelcome() {
// Print "Welcome to the site!" to the output
echo "Welcome to the site!";
}
// Call `printWelcome()`
printWelcome();
?>
Login to try C/C++/Java/PHP code in the editor
Conditional Function Declarations
किसी if block के अंदर एक function define करने का मतलब है कि function सिर्फ तभी exist में आता है जब वह block actually execute हो — PHP इसे conditionally, runtime पर register करता है, न कि unconditional function definition की तरह file parse होते ही।
उदाहरण: Conditional Function Declarations
<?php
// Declare `$defineIt`, set to `true`
$defineIt = true;
// Check whether `$defineIt`
if ($defineIt) {
// Define the function `sayHi` with no parameters
function sayHi() {
// Print "Hi!" to the output
echo "Hi!";
}
}
// Call `sayHi()`
sayHi();
?>
Login to try C/C++/Java/PHP code in the editor
- किसी conditional block के अंदर उसकी definition तक पहुँचने से पहले एक function call करना, जबकि सिर्फ top-level functions hoisted होते हैं।
- यह उम्मीद करना कि एक function इसके बाहर define किया गया कोई variable देखेगा, जबकि किसी function के अंदर variables local होते हैं जब तक पास न किए जाएँ।
- same function name दो बार define करना, जो एक fatal 'Cannot redeclare' error का कारण बनता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: