PHP Variable Scope In Hindi

📔 : PHP 🔗

Variable Scope , किसी भी Variable के लिए एक accessible area / portion होता है जहाँ किसी Variable को define करने के बाद उसे access किया जा सके। PHP Variable Scope तीन तरह के होते हैं - Local Variable , Global Variable And Static Variable .

  1. Local Variable

  2. Global Variable

  3. Static Variable

हालाँकि जयादातर PHP Variable single scope ही होते हैं और यह variables किसी included File से भी access किये जा सकते हैं।

Example-

<?php // File : test.php $x = "Variable Inside test.php file"; ?>

<?php include("test.php"); echo $x; ?>

Output :

Variable Inside test.php file

PHP Local Variable

Local Variables बो variables होते हैं जो किसी function के अंदर ही define किये जाते हैं और उसी function के अंदर ही accessible होते हैं , और न ही उस variable को उस function के बाहर access कर पाएंगे।

Example -

<?php $x = "External Variable"; function test() { $x = "Variable Inside Function"; echo $x; } test(); ?>

Note - आगर किसी फंक्शन के अंदर और बाहर एक ही नाम के variable define करेंगे तो function के अंदर वाले variable की priority ज्यादा होगी।

PHP Global Variable

Global Variables वो variables होते है जो किसी function के बाहर define किये जाते है और लेकिन function के अंदर भी access कर सकते है। हलाकि इन्हे access करने के लिए हमें या तो उस variable को globally define करना पड़ता है या तो Super Global variable के through access करना पड़ता है।

Example-

<?php $a = 1; $b = 2; function variable() { /* first access using $LOBALS variables */ echo "Access Variables Using Super GLOBALS variable: ".$GLOBALS['a'].','.$GLOBALS['b']; /* now first make these variables global*/ global $a, $b; echo "Access Global Variables : $a , $b"; } variable(); ?>

Output :

Access Variables Using Super GLOBALS variable: 1,2
Access Global Variables : 1 , 2

Note - अगर किसी function के अंदर हमें बाहरी variable को access करना है तो उस variable को बिना Super GLOBAL variable के access नहीं कर पाएंगे और अगर हम ऐसा करते है तो Error आएगी।

Example-

<?php $b = 2; function variable() { /* Access withowt making ot gloabl variable */ echo "Access Global Variable :".$b ; } variable(); ?>

Output :

Notice: Undefined variable: b in C:\xampp\htdocs\test\test.php on line 7
Access Global Variable :

PHP Static Variable

PHP में Static Variables हमें एक feature provide कराते हैं , Normally जब हम किसी function में variable को define करते हैं , और उसे कुछ value assign करने के बाद उस variable पर कुछ task perform करते हैं , तो Function Run होने के बाद उस variable में initial value ही रहती है।

Example-

<?php function static_variable() { $x = 1; echo "Before Increment :".$x ."<br>"; /* increment by one */ $x++; echo "After Increment :".$x; } /* Call the function */ static_variable(); /* Again Call the function */ static_variable(); ?>

Output :

Before Increment :1
After Increment :2
Before Increment :1
After Increment :2

तो आप जैसा देख सकते है जीतनी बार आप function को call कराएँगे variable $x initial value 1 ही रहेगी।

इसी कमी को static variable दूर करते है , means function के run होने के बाद last value variable को assign होगी तो दुबारा function call होने पर processing उसी value से start होती है।

Static variable define करने के लिए simply static keyword साथ define करते हैं।

Example -

<?php function static_variable() { static $x = 1; echo "Before Increment :".$x ."<br>"; /* increment by one */ $x++; echo "After Increment :".$x; } /* Call the function */ static_variable(); /* Again Call the function */ static_variable(); ?>

Output :

Before Increment :1
After Increment :2
Before Increment :2
After Increment :3

अब आप Clearly देख सकते है कि जब हमने function को दूसरी बार call किया तो value 2 से start हुई , मतलब जो variable लास्ट value hold किये था।

Note - इसके अलावा PHP हमें कुछ predefined variables भी provide करती है जिन्हे Super Global Variables कहते हैं। इन Global Variables को हम पूरे PHP script में कही भी access कर सकते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers