Results 1 to 8 of 8

Thread: why it displays outsider bar?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    why it displays outsider bar?

    HI please read this and tell me that why it outputs outsider bar?

    <?php
    $bar = “outside”;
    Function ab()
    {
    $bar = “inside”;
    }
    Ab();
    Echo $bar . “<br />”;

    ?>

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: why it displays outsider bar?

    Because of scope. A variable declared outside of a function (and not as a class member) has global scope. A variable declared within a function has a scope of that function, even if it has the same name as a globally scoped variable - it's a different, independent variable.

    This occurs so that function variables don't interfere with global ones, and so that your functions are reusable. Most commonly if you want to affect a global variable, you pass it (by value) as a function argument. In other situations you may want to pass by reference, or declare a global var within the function.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: why it displays outsider bar?

    To further what Samba noted, in order to get the function ab to recognize $bar, you can pass it in, or within your function, note that you're pulling in an outside variable:
    Code:
    <?php
    $bar = “outside”;
    Function ab($barParam)
    {
    $barParam = “inside”;
    }
    Ab($bar);
    Echo $bar . “<br />”;
    ?>

    Code:
    <?php
    $bar = “outside”;
    Function ab()
    {
    global $bar;
    $bar = “inside”;
    }
    Ab();
    Echo $bar . “<br />”;
    ?>
    I believe both of those will produce the same result... I know the second will will produce "inside" ... because I use that a lot... the first one I'm not 100&#37; confident in ... and I don't have a means to test it right this second.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: why it displays outsider bar?

    Nope, the first one won't output "inside"; this would:
    Code:
    $bar = “outside”;
    Function ab($barParam)
    {
    return “inside”;
    }
    $bar = Ab($bar);
    Echo $bar . “<br />”;
    ...or this:
    Code:
    $bar = “outside”;
    Function ab(&$barParam)
    {
    $barParam = “inside”;
    }
    Ab($bar);
    Echo $bar . “<br />”;

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: why it displays outsider bar?

    Thanks for the clarification... makes sense... the & before the $ is akin to the byref in VB... that explains some of the problems I've had in the past, and why I always end up using the global method... that will clean up a lot for me - I've only done PHP for personal sites, so I've never followed up on the why's abnd whatfors before...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: why it displays outsider bar?

    its little complicated to understand.

    let me ask something, let me have a dry run of this program, please guide me with this little clicking point.
    In first step a global variable will be created and assigning it a value 'Inside' right
    then function will be defined
    and then function will be called ok.
    the problem to understand is that
    When the function will be called so its body will be executed right, so after execution of body, where the control will go? will it go to again AB(); function or will it go to next Echo statement? Did you understand?

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: why it displays outsider bar?

    No... AB() will ONLY run WHEN IT IS CALLED... just because you define it, doesn't mean it runs right away. No different than any other procedure based language.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: why it displays outsider bar?

    @ Samba and @ techgnome

    I'm totally confused that whats going on

    Would you both please make a dry run of my program step by step? I'm mind is not picking it at all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width