Results 1 to 5 of 5

Thread: How to use Variable scope in PHP?

  1. #1
    Join Date
    Aug 2006
    Posts
    209

    How to use Variable scope in PHP?

    Hello friends,
    I am new to the PHP programming language. So I don't know much about it. I just went through the term variable scope. Before that I have done Core Java and C++ programming language, so I can guess what can it means but I am not sure about it. Please can anyone tell me how to use Variable scope in PHP? Help me as soon as possible.
    Blessings to you

  2. #2
    Join Date
    Mar 2008
    Posts
    672

    Re: How to use Variable scope in PHP?

    In PHP there is something called scope, which relates to variables. The scope of a variable is the context within which it is defined. The scope is the area in which the variable is declared. You set your scope with curly braces (the { and } characters) in PHP. You should also know the basic thing that functions keep their own sets of variables that are distinct from those of the page and of other functions. The variables defined in a function, including its parameters, are not accessible outside the function.

  3. #3
    Join Date
    Mar 2008
    Posts
    349

    Re: How to use Variable scope in PHP?

    In PHP, mostly all the part has only a single scope. Variables defined outside a function are not accessible inside the function by default. The single scope spans included and required files as well :
    PHP Code:
    $a 4;

    function 
    call( ) {
      
    $a += 3;
    }

    call( );
    echo 
    $a

  4. #4
    Join Date
    Aug 2006
    Posts
    235

    Re: How to use Variable scope in PHP?

    Any variable that you are using inside a function is by default limited to the local function scope. You can check the following example :
    PHP Code:
    <?php
    $a 
    3

    function 
    test()

        echo $
    3


    test();
    ?>
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  5. #5
    Join Date
    Nov 2008
    Posts
    996

    Re: How to use Variable scope in PHP?

    You can use the global for the variable scope in PHP, like I have done in following code :
    PHP Code:
    <?php
    $a 
    3;
    $b 5;

    function 
    Sum()
    {
        global 
    $a$b;

        
    $b $a $b;


    Sum();
    echo 
    $b;
    ?>

Similar Threads

  1. Replies: 3
    Last Post: 08-01-2011, 06:20 AM
  2. What is the scope of variable in Visual basic 6.0?
    By Mullaiiii in forum Software Development
    Replies: 4
    Last Post: 02-01-2011, 08:18 AM
  3. Problem solving scope of variable
    By Maya Angelou in forum Software Development
    Replies: 5
    Last Post: 23-03-2010, 10:10 AM
  4. Replies: 2
    Last Post: 28-08-2009, 07:51 PM
  5. Replies: 3
    Last Post: 25-04-2009, 11:34 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,022,974.49391 seconds with 17 queries