Results 1 to 7 of 7

Thread: Function Variables -[RESOLVED]-

  1. #1

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Resolved Function Variables -[RESOLVED]-

    If I have a function and the variables inside I want to be private to that function is this possible? If so how do I make them private or are they by default?
    Last edited by Electroman; Nov 13th, 2004 at 10:36 PM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    Do you mean functions in a calss? If so in PHP 4 there is no private/public but it has been introduced in PHP 5.

    If you are talking about variable scope then this is how it works.
    • All variables used inside a function have local scope.
    • Local variables with the same name as global variables elsewhere in a script are treated as different entities with the exception of super globals
    • To use a global variable declared outside a function you must either declar it inside the function using the global keyword or access it through the super global array $GLOBALS.


    Here's some examples:
    PHP Code:
    <?php
    $global_var 
    'no function called';

    echo(
    "Calling myfunction: ");
    myfunction();

    echo(
    "\$global_var=$global_var");

    echo(
    "Calling myfunction2: ");
    myfunction2();

    echo(
    "\$global_var=$global_var");

    echo(
    "Calling myfunction3: ");
    myfunction3();

    echo(
    "\$global_var=$global_var");
        
    function 
    myfunction()
    {
        
    /* this creates a new variable with called - $global_var with local scope */
        
    $global_var 'myfunction called'
    }

    function 
    myfunction2()
    {
        
    /* we can declare a global variable here */
        
    global $global_var;

        
    $global_var 'myfunction2 called';
    }

    function 
    myfunction3()
    {
        
    /* we can also use the $GLOBALS super global array */
        
    $GLOBALS['global_var'] = 'myfunction3 called';
    }
    ?>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    The global thing isn't like extern (in C++) is it? Like in the following code I've highlighted two of them, are they using the same space? I'm doughting this but hopeing at the same time. Just cos I have variables that I might want to be global and also used in more than one function I guess to do that I'd have to use the super global.
    PHP Code:
    <?php
    $global_var 
    'no function called'; <---- This one

    echo("Calling myfunction: ");
    myfunction();

    echo(
    "\$global_var=$global_var");

    echo(
    "Calling myfunction2: ");
    myfunction2();

    echo(
    "\$global_var=$global_var");

    echo(
    "Calling myfunction3: ");
    myfunction3();

    echo(
    "\$global_var=$global_var");
        
    function 
    myfunction()
    {
        
    /* this creates a new variable with called - $global_var with local scope */
        
    $global_var 'myfunction called'
    }

    function 
    myfunction2()
    {
        
    /* we can declare a global variable here */
        
    global $global_var;

        
    $global_var 'myfunction2 called'; <---- This one
    }

    function 
    myfunction3()
    {
        
    /* we can also use the $GLOBALS super global array */
        
    $GLOBALS['global_var'] = 'myfunction3 called';
    }
    ?>
    I think for now all I needed to know was that my function variables were private .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    Originally posted by Electroman
    The global thing isn't like extern (in C++) is it?
    Yes
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Posted by visualAd
    Yes
    I knew I shouldn't have asked a negative question . So it is like extern?
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It is like extern, except that it is mandatory if you want to access a global. That's because of the lack of variable declarations in PHP.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    Originally posted by Electroman
    I knew I shouldn't have asked a negative question . So it is like extern?
    Sorry - I misread. It is like extern.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

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