PHP Function कोड का एक ब्लॉक होता है, जिसे बार-बार उपयोग किया जा सकता है। फंक्शन का मुख्य उद्देश्य कोड को modular और reusable बनाना है, ताकि इसे आसानी से manage और execute किया जा सके।
PHP Function Syntax:
<?php
function functionName($parameter1, $parameter2, ...) {
// फंक्शन का कोड
return $value; // वैकल्पिक (optional)
}
?>PHPWhere:
- functionName: फंक्शन का नाम।
- $parameter1, $parameter2: फंक्शन को पास किए जाने वाले arguments।
- return $value: फंक्शन द्वारा return की गई value।
Exmple of functions in php:
Simple Function:
<?php
function sayHello() {
echo "Hello, PHP!";
}
sayHello();
?>PHPFunction With Parameter:
<?php
function greet($name) {
echo "Welcome, $name!";
}
greet("Vikas"); // Output: Welcome, Vikas!
?>PHPFunction With Return Value:
<?php
function addNumbers($a, $b) {
return $a + $b;
}
$result = addNumbers(10, 20);
echo "Sum: $result";
// Output: Sum: 30
?>PHPFunction With Default Parameters:
<?php
function greet($name = "World") {
echo "Hello, $name!";
}
greet();
// Output: Hello, World!
greet("Vikas");
// Output: Hello, Vikas!
?>PHPFunction with Multiple Parameters:
<?php
function multiply($a, $b) {
return $a * $b;
}
echo "Product: " . multiply(5, 4);
// Output: Product: 20
?>PHPTypes of Functions In php:
PHP में फंक्शन (Functions) को मुख्यतः दो प्रकारों में वर्गीकृत किया जा सकता है:
- Built-in Functions
- User Defined Function
Built-in Functions:
ये वे फंक्शन होते हैं, जो PHP द्वारा पहले से ही प्रदान किए गए होते हैं। इनका उपयोग सीधे कोड में किया जा सकता है। PHP में हजारों इनबिल्ट फंक्शन होते हैं, जो विभिन्न कार्यों के लिए उपयोगी होते हैं।
Some Important In-Built Function:
स्ट्रिंग फंक्शन (String Functions)
जैसे: strlen(), strtoupper(), strtolower(), substr(), str_replace() आदि।
एरे फंक्शन (Array Functions)
जैसे: array_push(), array_pop(), count(), array_merge(), sort() आदि।
डेट और समय फंक्शन (Date and Time Functions)
जैसे: date(), time(), strtotime(), mktime() आदि।
फ़ाइल हेंडलिंग फंक्शन (File Handling Functions)
जैसे: fopen(), fwrite(), fread(), file_get_contents() आदि।
मैथ फंक्शन (Math Functions)
जैसे: abs(), round(), ceil(), floor(), sqrt() आदि।
डाटाबेस फंक्शन (Database Functions)
जैसे: mysqli_connect(), mysqli_query(), mysqli_fetch_assoc() आदि।
User Defined Functions:
ये फंक्शन User द्वारा Define किए जाते हैं। जब किसी विशेष कार्य को बार-बार करने की आवश्यकता होती है, तो इन्हें उपयोग किया जाता है। इन्हें हम अपनी आवश्यकता के अनुसार बनाते हैं।
<?php
function functionName($parameter1, $parameter2) {
// फंक्शन के अंदर का कोड
return $result;
}
// फंक्शन को कॉल करना
functionName(value1, value2);
?>PHPAnonymous Functions:
इन फंक्शन्स का कोई नाम नहीं होता और इन्हें वैरिएबल में स्टोर किया जा सकता है। इन्हें क्लोज़र भी कहा जाता है।
<?php
$greet = function($name) {
return "Hello, " . $name . "!";
};
echo $greet("Vikas");
?>PHPRecursive Functions:
ये वे फंक्शन हैं, जो खुद को ही बार-बार कॉल करते हैं। इन्हें मुख्यतः किसी समस्या को छोटे भागों में हल करने के लिए उपयोग किया जाता है।
<?php
function factorial($n) {
if ($n == 0) {
return 1;
} else {
return $n * factorial($n - 1);
}
}
echo "factorial: " . factorial(5);
?>PHPConclusion:
PHP में फंक्शन को दो मुख्य श्रेणियों में बांटा जा सकता है: इनबिल्ट फंक्शन और यूज़र-डिफाइंड फंक्शन। इनके अलावा, एनोनिमस फंक्शन और रीकर्सिव फंक्शन भी महत्वपूर्ण हैं। फंक्शन्स कोड को पुन: उपयोगी (Reusable) और सुव्यवस्थित (Organized) बनाते हैं।

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.