Results 1 to 8 of 8

Thread: variables

  1. #1

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    variables

    i am pretty new to PHP and am having a little problem.
    my code is like this.

    Code:
    function thing()
    {
           include("global_variables.php");
           echo($variable1);
    }
    
    function thing2()
    {
           include("global_variables.php");
           echo($variable2);
    }
    this is not how i want it to be. the question is, is it possible to include the global variables file just once to make it work with all the functions?

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: variables

    well there are no global variables. The best idea is to have a configuration file, or settings file where are the configs are made.

    A config file might include MySQL connections, variables for site title, or other things. And on all the pages that you need the mysql connections, you would include the config file.

    so for ex:
    PHP Code:
    //CONFIG FILE:

    $mysitetitle "My Cool Site"
    And my page:

    PHP Code:
    <?php include("config.php"); ?>

    <title><?php echo $mysitetitle?></title>
    ... html
    My usual boring signature: Something

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: variables

    Dclamp, all variables initialised outside a function or class are global.
    To use these within a function you must import them using the global keyword. Otherwise, you create a local variable of the same name, which is discarded once it goes out of scope.

    PHP Code:
    $bar 10;
    echo 
    $bar;

    function 
    foo()
    {
      global 
    $bar;
      echo ++
    $bar;
    }

    echo 
    $bar

  4. #4
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: variables

    So i can use $bar on another page without including it?
    My usual boring signature: Something

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: variables

    No. What would give you that idea?

  6. #6
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: variables

    well then it is really not required to set the variables as global then... right?
    My usual boring signature: Something

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: variables

    I don't understand your question. Look up variable scope in the online manual if you want a more thorough explanation and let's leave this thread for the thread starter.

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: variables

    Quote Originally Posted by penagate
    I don't understand your question. Look up variable scope in the online manual if you want a more thorough explanation and let's leave this thread for the thread starter.
    He has clearly read my last PHP post out of context
    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