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

PHP Functions Introduction

एक function एक recipe card जैसा है जिसे आप एक बार लिखते हैं और जब चाहें इस्तेमाल करते हैं। आप इसे एक नाम देते हैं, और फिर आप उन्हीं steps को बिना दोबारा लिखे बार-बार चला सकते हैं।
Syntax
php
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
<?php
// Define the function `greet` with no parameters
function greet() {
    // Print "Hello!" to the output
    echo "Hello!";
}
// Call `greet()`
greet();
?>

Function Naming Rules

Function names को एक letter या underscore से शुरू होना चाहिए, और जबकि PHP खुद आपके इस्तेमाल किए casing की परवाह नहीं करता, consistent रहना — पूरे project में camelCase या snake_case — किसी codebase को scan और predict करना कहीं आसान बनाता है।

उदाहरण: Function Naming Rules

php
<?php
// Define the function `calculateTotal` with no parameters
function calculateTotal() {
    // Return `100` from this function
    return 100;
}
// Print `calculateTotal()` to the output
echo calculateTotal();
?>

Functions के अंदर Variable Scope

किसी function के अंदर बनाया गया कोई भी variable सिर्फ उस function के खुद के execution के लिए exist करता है और return होते ही गायब हो जाता है — बाहर का code इसे नहीं देख सकता।

किसी function के अंदर आसपास की global scope से एक variable पढ़ने या modify करने के लिए, आपको पहले इसे explicitly global keyword से declare करना होगा।

उदाहरण: Variable Scope inside Functions

php
<?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();
?>

बिना Arguments वाले Functions

किसी function को उपयोगी होने के लिए parameters की ज़रूरत नहीं — बहुत सारे functions हर बार call होने पर बस एक fixed task perform करते हैं या एक fixed message print करते हैं, अपना काम करने के लिए बाहर से कुछ भी नहीं चाहिए।

उदाहरण: Functions with No Arguments

php
<?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();
?>

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
<?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();
?>
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. किसी conditional block के अंदर उसकी definition तक पहुँचने से पहले एक function call करना, जबकि सिर्फ top-level functions hoisted होते हैं।
  2. यह उम्मीद करना कि एक function इसके बाहर define किया गया कोई variable देखेगा, जबकि किसी function के अंदर variables local होते हैं जब तक पास न किए जाएँ।
  3. same function name दो बार define करना, जो एक fatal 'Cannot redeclare' error का कारण बनता है।

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.