आज हम php सिखने की इस सीरीज में Variable Scope kya hain | what is Variable Scope in Hindi, type of PHP Variable Scope in Hindi आदि के बारे में पढ़ेंगे। इस टॉपिक को पढ़ने से पहले में Hindi me IT में आपका बहुत बहुत अभिनान्दन हैं।
Variable Scope kya hain? | what is Variable Scope in Hindi?
php programming language में किसी भी variable को एक program में उसकी range के आधार पर परिभाषित किया जा सकता हैं। अर्थात variable का वह area जिसमें variable को access किया जा सकता हैं। उस area को variable का Scope कहते हैं।
दूसरे शब्दों में कहे तो एक variable का दायरा program का वह भाग हैं जिसके भीतर variable को define किया गया हैं और इसे access भी किया जा सकता हैं।
- Introduction php advantage and disadvantage in hindi
- what is php data type and echo, print used php in hindi
php में scope कितने प्रकार का होता हैं? | type of PHP Variable Scope in Hindi?
अगर php में variable के scope की बात करें तो बो तीन प्रकार का होता हैं।
- Local variable
- Global variable
- Static variable
Local Variable Scope kya hain? | what is Local Variable Scope in PHP in Hindi?
php programming language में Local Variable वे होते हैं जो किसी function के अंदर define किये जाते हैं। ऐसे variable उस function के लिए Local Variable होते हैं। इन variable को function के ander किसी विशेष काम को पूरा करने के लिए डिफाइन किया जाता हैं।
इन variable को केवल function के अंदर ही access किया जा सकता हैं। इन वेरिएबल को function के बाहर access नहीं किया जा सकता हैं। क्योकिं इसके पास local scope होता हैं।
Example:-
<?php
function local_Scope_variable()
{
$num = 45; //local Scope variable
echo "Local variable declared inside the function is: " $num;
}
local_Scope_variable()
?>
Output:-
Local variable declared inside the function is: 45
Note :- अगर कोई variable function के बाहर और function के अंदर एक ही नाम से डिफाइन हैं तो ये दोनों variable अलग-अलग होंगे
Example:-
<?php
function local_Scope_variable()
{
$language = "PHP";
echo "Web development language: " .$lang;
}
local_Scope_variable();
//using $language (local variable scope) outside the function will generate an error
echo $language;
?>
Output:-
Web development language: PHP
Notice: Undefined variable: language in D:\xampp\htdocs\program\p3.php on line 9
Global Variable Scope kya hain? | what is Global Variable Scope in PHP in Hindi?
php programming language में global Variable वे होते हैं जो किसी function के बाहर define किये जाते हैं। ऐसे variable पुरे program में कहीं भी Access किया जा सकता हैं।
global Variable को किसी भी function के अंदर access करने के लिए उस variable से पहले global keyword का use किया जाता हैं।
इन variable को function के बाहर बीना किसी keyword access किया जा सकता हैं।
global Vriable को कुछ Example की हेल्प से समझते हैं।
<?php
$name = "Hindi me IT"; //This is Global Variable
function global_varable()
{
global $name;
echo "Variable inside the function: ". $name;
echo "</br>";
}
global_var();
echo "Variable outside the function: ". $name;
?>
Output:-
Variable inside the function: Hindi me IT
Variable outside the function: Hindi me IT
Note:- यदि आप global keyword का use किये बीना आप global variable का use करते हैं। तो आपको एक error मिलेगी की variable undefined
Example-
<?php
$name = "Hindi me IT"; //this is global variable
function global_variable()
{
echo "Variable inside the function: ". $name;
echo "</br>";
}
global_variable();
?>
Output
Notice: Undefined variable: name in D:\xampp\htdocs\program\p3.php on line 6
Variable inside the function:
$GLOBALS का use करके
php में global variable का function के अंदर उपयोग करने के दूसरा तरीका $GLOBALS array का use करके।
Example –
<?php
$number1 = 9; //This is global variable
$number2 = 6; //This is global variable
function global_var()
{
$sum = $GLOBALS['number1'] + $GLOBALS['number2'];
echo "Sum of global variables is: " .$sum;
}
global_var();
?>
Output
Sum of global variables is: 18
- अगर php program में local और global variable एक ही नाम से, तो function के अंदर local variable की priority अधिक होगी।
Example –
<?php
$var = 10; //this is global variable
function my_test()
{
$var = 15;
echo "value of var: " .$var;
}
my_test();
?>
Output-
Value of var: 7
Static Variable Scope kya hain? | what is Static Variable Scope in PHP in Hindi?
php प्रोग्रामिंग language की एक बहुत ही अच्छी विशेषता हैं। की जब php execution complete हो जाता हैं, तो memory मुक्त हो जाती हैं।
कभी-कभी हमें आपने logic के अनुसार function के execution complete होने के बाद भी variable की value को store करने की जरूरत होती हैं। इसलिए variable scoping का एक अन्य feature static variable हैं।
इस variable को define करने के लिए variable के नाम से static keyword का उपयोग करते हैं। इसी वेरिएबल को php में static variable कहते हैं।
static variable केवल local function में ही exist करते हैं। ये variable execution कम्पलीट होने के बाद भी अपनी memory को free नहीं करते हैं।
इसे एक example से समझते हैं।
<?php
function static_variable()
{
static $num1 = 3; //this is static variable
$num2 = 6; //this is Non-static variable
//increment in non-static variable
$num1++;
//increment in static variable
$num2++;
echo "Static: " .$num1 ."</br>";
echo "Non-static: " .$num2 ."</br>";
}
//first function call
static_variable();
//second function call
static_variable();
?>
Output–
Static: 4
Non-static: 7
Static: 5
Non-static: 7
इस output पर आपने ध्यान दिया हैं तो आप सीख रहे हैं की वेरिएबल $num1 function के प्रत्येक call होने के बाद नियमित रूप से value में वृद्धि हो जाती हैं। जबकि वेरिएबल $num2 नहीं करता हैं। ऐसा इसलिए क्योकिं variable $num1 एक static वेरिएबल हैं। इस लिए प्रत्येक फंक्शन कॉल के execution के बाद अपनी memory खली नहीं करता हैं।
refrence By:-https://www.w3schools.com/php/php_variables_scope.asp
-
variable का स्कोप क्या होता हैं
variable का वह area जिसमें variable को access किया जा सकता हैं। उस area को variable का Scope कहते हैं।
-
वेरिएबल स्केपिंग कितने प्रकार के होते हैं?
अगर php में variable के scope की बात करें तो बो तीन प्रकार का होता हैं।
Conclusion
इस आर्टिल्स में हमने सीखा Variable Scope kya hain | what is Variable Scope in Hindi, type of PHP Variable Scope in Hindi
दोस्तों मैं आशा करता हूँ कि आपको ये जानकारी पसंद आयी होगी और यदि ये जानकारी आपको पसंद आयी हो तो इस जानकारी को अपने दोस्तों के साथ शेयर करे ताकि उनको भी Variable Scope kya hain | what is Variable Scope in Hindi, type of PHP Variable Scope in Hindi को जानने हेल्प मिलेगी।
अगर आपको अभी भी Variable Scope kya hain | what is Variable Scope in Hindi, type of PHP Variable Scope in Hindi से संबंधित कोई भी प्रश्न या Doubt है तो आप कमेंट्स के जरिए हमसे पुछ सकते है। मैं आपके व्दारा पूछा गये सभी सवालों का जवाब दूँगा।
अगर आपको हमारे व्दारा दी जानकारी पसंद आती हैं और आप ,Computer Science, इंटरनेट, IT, मॉडल टेक्नोलॉजी और new जानकारी पढ़ना अच्छा लगता हैं. तो आप हमारे इस ब्लॉग को जरूर सब्सक्राइब कर कर लो | अगर आपका हमको कोई नया सुझाब देना हैं तो वह भी बताये।