Local Variable, Global Variable Use In Function : 3rd Day Lecture of PHP


Local Variable, Global Variable Use

Sample 1 : Simple Function of Local And Global Variable

This is Big Number: 35
This number is less than Big Number : 15

Sample 2 : Rules of Local And Global Variables

  1. We can not use Local Variables outside the function.
  2. We can not use Global Variables inside the function Directly.
  3. We can use Global Variables inside the function by $GLOBALS['Variable'].
    • But in $GLOBALS['Variable'], you can contain only one single variable.
    • For different Variables you will create separate $GLOBALS['x']
    • In $GLOBALS all spellings are Capital
    • Variable will be written within single quote.
  4. As you know; after function completion, local variables will be finished, so you need to keep in mind these rules

Sample 3 : Global Variable Use inside the Function.

What is Your Name And Age?
My Name is Samee
And My Age is 22 And my double age will be 44
Tell me your complete bio data?
My Name is Samee, age is 22, and My height is 5.3.

Sample 4 : Local Variable use Out Side the Function.

Sorry! We can not use Local Variable out side the Function in PHP.


   <html>
 <head>
  <title>Local and Global Variables Use In Function : 3rd Day Lecture of PHP</title>
 </head>
 <body>
  <h1 align="center">Local Variable, Global Variable Use</h1>
   <h2 align="left">Sample 1 : Simple Function of Local And Global Variable</h2>
    <?php 
     $a=5;
     $b=10;
     function add()
     {
     $c=15;
     $d=20;
     echo "This is Big Number: " . ($c+$d);
     echo "<br />";
     }
     add();
     
     echo "This number is less than Big Number : ". ($a+$b);
     
     echo "<br />";
    ?>
    
   <h2 align="left">Sample 2 : Rules of Local And Global Variables</h2>
    <?php
    echo 
     '<ol>
      <li>We can not use Local Variables outside the function.</li>
      <li>We can not use Global Variables inside the function Directly.</li>
      <li>We can use Global Variables inside the function by $GLOBALS[\'Variable\'].
       <ul>
       <li>But in $GLOBALS[\'Variable\'], you can contain only one single variable.</li>
       <li>For different Variables you will create separate $GLOBALS[\'x\']</li>
       <li>In $GLOBALS all spellings are Capital</li>
       <li>Variable will be written within single quote.</li>
       </ul></li>
      <li>As you know; after function completion, local variables will be finished, so you need to keep in mind these rules</li>
     </ol>' //backslash operator is used to give output of single quote of double quote in double quote. 
    ?>
    
     <h2 align="left">Sample 3 : Global Variable Use inside the Function.</h2>
    <?php
    $name="Samee";
    $age=22;
    $hight=5.3;
    function msr()
    {
     echo "<b>What is Your Name And Age?</b> <br />";
     echo "My Name is {$GLOBALS['name']}";
     echo "<br />";
     echo " And My Age is " . $GLOBALS['age'] . " And my double age will be " . ($GLOBALS['age']*2) . "<br />";
    }
    msr();
     echo "<b>Tell me your complete bio data?</b> <br />";
     echo "My Name is $name, age is $age, and My height is $height."
    ?>
    
   <h2 align="left">Sample 4 : Local Variable use Out Side the Function.</h2>
    <?php echo "<b>Sorry! We can not use Local Variable out side the Function in PHP.</b>" ?>
 </body>
</html>

Share this article :

Post a Comment

Give your reviews about this blog. Leave your comments. What do you think about this post?

Comments Guidelines:

1) I love to read comments, but please avoid spam.

2) Like this blog, share your insights, and spread the word.

3) You can use basic HTML/xHTML tags.

4) All comments are DoFollow—please use the section professionally.

5) Kindly mention your name or brand below your comment.

6) Constructive suggestions for improvement are always welcome.

7) Do not forget to explore our main agency platform at pingbooster.site.

Thanks for visiting Samee Articles.

========================================

Explore custom digital solutions and WordPress plugins at PingBooster Plugins.

Best Regards,

Samee U. Feroz

 
Copyright © 2013-2016. Samee Articles - All Rights Reserved
Proudly powered by Blogger