PHP in Hindi Variables




एक Variable Data Type के लिए एक Holder होता है. इसलिए इसके Type के आधार पर, एक Variable Numbers, Strings, Booleans, Objects के Resources को Hold कर सकता है या यह NULL कर सकता है .

PHP में सभी Variable एक Sign "$" से शुरू होते हैं और इसमे "=" operator का उपयोग करके Value को Assign किया जा सकता है . Dollar Sign तकनीकी रूप से Variable के Name का हिस्सा नहीं है लेकिन PHP Parser के पहले वर्ण के रूप में इस तरह के Variable को पहचानना आवश्यक होता है .

PHP मे एक और महत्वपूर्ण बात यह है कि सभी Statements को एक semicolon ";" के साथ ही समाप्त करना चाहिए. PHP मे हमे Variable के Type को Specify करने की ज़रूरत नहीं है क्योंकि यह Assign की गई Value से Data Type को लेता है.

एक Variable को Declare करने के लिए आपको इसे अपनी Script में शामिल करना होगा. आप एक Variable को Declare कर सकते हैं और उसे एक ही Statement मे एक Value के साथ Assign कर सकते है.

Rules for Variable Declaration

  • Variable Numbers से शुरू नहीं होता है.

  • Variable $ Sign से शुरू होता है और Variable Name के साथ.

  • Variable Name Letter या Underscore Sign से शुरू होते है उसके बाद Underscore Number या Characters होते है.

  • Variable को alpha-numeric Characters का उपयोग करके और केवल Underscores करके Define किया जा सकता है.

PHP Variable Scope

Scope केवल Rules के अलावा कुछ नहीं होता है जब दो अलग-अलग Places के नाम का अर्थ समान अर्थ है. PHP मे Places को Define करने के लिये तीन Variable Scope है.

  • Local Variable Scope

  • Global Variable Scope

  • Static Variable Scope

Local Variable Scope

Local Variable को Function के भीतर Define और Access किया जाता है. ये Function के बाहर Accessible नहीं है.

For Example

<!DOCTYPE html>
<html>   
   <head>
      <title>Local Variables Example</title>
   </head>   
   <body>
      <?php 
         function myTest() {
            $x = 6; 
            echo "<p>Variable x inside function is: $x</p>";
         } 
         myTest();
         echo "<p>Variable x outside function is: $x</p>"; 
      ?>
   </body>
</html>

Output

Variable x inside function is: 6
Variable x outside function is:

Global Variable Scope

Global Variable को Function के बाहर Define किया जाता है. Function के बाहर Script में कहीं भी आप Global Variable का उपयोग कर सकते है. लेकिन Global Variable को Function के अंदर भी Access किया जा सकता है Variable को Declared होने से पहले Keyword Global का उपयोग करके.

For Example

<!DOCTYPE html>
<html>   
   <head>
      <title>Global Variables Example</title>
   </head>   
   <body>
      <?php 
         $x = 6; 
         function myTest() {    
            echo "<p>Variable x inside function is: $x</p>";
         } 
         myTest();
         echo "<p>Variable x outside function is: $x</p>"; 
      ?>
   </body>
</html>

Output

Variable x inside function is:
Variable x outside function is: 6

Static Variable Scope

आम तौर पर जब कोई Function Completed Executed होता है तो उसके सभी Variables Delete कर दिये जाते है. हालांकि कभी-कभी हम एक Local Variable को हटाना नहीं चाहते है. हमें इसे और Further के लिए चाहिए होता है . ऐसा करने के लिए आपको Static Keyword का उपयोग करके और पहले Variable को Declare करना होता है.

For Example

<!DOCTYPE html>
<html>   
   <head>
      <title>Static Variables Example</title>
   </head>   
   <body>
      <?php 
         $a = 70; 
            $b = 105; 
             
            function myVariables() { 
            static $a, $b; 
            $b = $a + $b; 
            }
              
            myVariables();
            echo "The valuse of b is:". $b; 
      ?>
   </body>
</html>

Output

The valuse of b is:105