Accessing variables outside a function... possible?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leigh Riley

    Accessing variables outside a function... possible?

    Hi,

    Can someone tell me if the following is possible?

    I have a file containing some STATIC variables, and some functions e.g.
    -------------------------------------------------
    $ARCHIVE_INPUT_ FILES = false;
    $ARCHIVE_OUTPUT _FILES = false;
    $ARCHIVE_CONFIG _FILES = false;
    $HTML_BODY_BACK GROUND = "gfx/gradient.gif";

    function firstOne
    {
    // Do something e.g.
    echo $HTML_BODY_BACK GROUND;
    }
    -------------------------------------------------

    The functions CANNOT see the variables I have declared at the top, so is
    there anything I can do make the functions see the variables i.e.
    declare them differently?

    Thanks!

    - Lee

  • Jan Pieter Kunst

    #2
    Re: Accessing variables outside a function... possible?

    In article <3f732ea6$1_2@n ews3.es.net>,
    Leigh Riley <dontEmailMeDir ectly@home.org> wrote:
    [color=blue]
    > function firstOne
    > {
    > // Do something e.g.
    > echo $HTML_BODY_BACK GROUND;
    > }
    > -------------------------------------------------
    >
    > The functions CANNOT see the variables I have declared at the top, so is
    > there anything I can do make the functions see the variables i.e.
    > declare them differently?[/color]

    function firstOne {
    global $HTML_BODY_BACK GROUND;
    echo $HTML_BODY_BACK GROUND;
    }


    or declare them as constants:

    define('HTML_BO DY_BACKGROUND', 'gfx/gradient.gif');

    function firstOne {
    echo HTML_BODY_BACKG ROUND; // no $ sign == constant
    }


    JP

    --
    Sorry, <devnull@cauce. org> is een "spam trap".
    E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

    Comment

    • Tom Thackrey

      #3
      Re: Accessing variables outside a function... possible?


      On 25-Sep-2003, Leigh Riley <dontEmailMeDir ectly@home.org> wrote:
      [color=blue]
      > Can someone tell me if the following is possible?
      >
      > I have a file containing some STATIC variables, and some functions e.g.
      > -------------------------------------------------
      > $ARCHIVE_INPUT_ FILES = false;
      > $ARCHIVE_OUTPUT _FILES = false;
      > $ARCHIVE_CONFIG _FILES = false;
      > $HTML_BODY_BACK GROUND = "gfx/gradient.gif";
      >
      > function firstOne
      > {
      > // Do something e.g.
      > echo $HTML_BODY_BACK GROUND;
      > }
      > -------------------------------------------------
      >
      > The functions CANNOT see the variables I have declared at the top, so is
      > there anything I can do make the functions see the variables i.e.
      > declare them differently?[/color]

      declare them as global in the funciton

      global $ARCHIVE_INPUT_ FILES;



      --
      Tom Thackrey

      Comment

      • Pedro

        #4
        Re: Accessing variables outside a function... possible?

        [ not posted to alt.php ]

        Leigh Riley wrote:[color=blue]
        >Hi,
        >
        >Can someone tell me if the following is possible?
        >
        >I have a file containing some STATIC variables, and some functions e.g.
        >-------------------------------------------------
        >$ARCHIVE_INPUT _FILES = false;
        >$ARCHIVE_OUTPU T_FILES = false;
        >$ARCHIVE_CONFI G_FILES = false;
        >$HTML_BODY_BAC KGROUND = "gfx/gradient.gif";
        >
        >function firstOne
        >{
        > // Do something e.g.
        > echo $HTML_BODY_BACK GROUND;
        >}
        >-------------------------------------------------
        >
        >The functions CANNOT see the variables I have declared at the top, so is
        >there anything I can do make the functions see the variables i.e.
        >declare them differently?[/color]

        Well ... if they're STATIC variables make them constants :-)
        That way you can see them inside the functions

        <?php
        define('ARCHIVE _INPUT_FILES', false);
        define('ARCHIVE _OUTPUT_FILES', false);
        define('ARCHIVE _CONFIG_FILES', false);
        define('HTML_BO DY_BACKGROUND', 'gfx/gradient.gif');
        ?>


        Failing that you can declare the variables global in the function:
        <?php
        function firstOne
        {
        global $ARCHIVE_INPUT_ FILES, $ARCHIVE_OUTPUT _FILES;
        global $ARCHIVE_CONFIG _FILES, $HTML_BODY_BACK GROUND;

        // Do something e.g.
        echo $HTML_BODY_BACK GROUND;
        }
        ?>

        or maybe access the $GLOBAL array inside the function
        <?php
        function firstOne
        {
        // Do something e.g.
        echo $GLOBALS['HTML_BODY_BACK GROUND'];
        }
        ?>

        Happy Coding :-)


        --
        I have a spam filter working.
        To mail me include "urkxvq" (with or without the quotes)
        in the subject line, or your mail will be ruthlessly discarded.

        Comment

        Working...