how do you access an array in an included/required field?

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

    #1

    how do you access an array in an included/required field?

    I have a large array that would be messy to include in the file "main.php"
    itself and would prefer to include/require it. Say I have the following
    line in "main.php":

    require_once('/arr.php');

    And the "arr.php" file looks something like this:

    <?php
    array(1)='ok1@O k.com';
    array(2)='ok2@O k.com';
    array(3)='ok3@O k.com';
    array(4)='ok4@O k.com';
    array(5)='ok5@O k.com';
    array(6)='ok6@O k.com';
    array(7)='ok7@O k.com';
    array(8)='ok8@O k.com';
    array(9)='ok9@O k.com';
    array(10)='ok10 @Ok.com';
    ?>

    Would I be able to reference the array AS IF I had declared the array in
    "main.php"?

    I am a programmer but new to PHP.

    Many thanks.


  • Pedro Graca

    #2
    Re: how do you access an array in an included/required field?

    NotGiven wrote:[color=blue]
    > I have a large array that would be messy to include in the file "main.php"
    > itself and would prefer to include/require it. Say I have the following
    > line in "main.php":
    >
    > require_once('/arr.php');
    >
    > And the "arr.php" file looks something like this:
    >
    ><?php
    > array(1)='ok1@O k.com';[/color]
    [snipped][color=blue]
    > ?>
    >
    > Would I be able to reference the array AS IF I had declared the array in
    > "main.php"?
    >
    > I am a programmer but new to PHP.[/color]

    What hapenned when you tried it?

    I guess your problem is with the require_once(). Are you sure you have
    arr.php on the root dir?
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Pedro Graca

      #3
      Re: how do you access an array in an included/required field?

      Pedro Graca wrote:[color=blue]
      > NotGiven wrote:[color=green]
      >> <?php
      >> array(1)='ok1@O k.com';[/color][/color]
      [color=blue]
      > I guess your problem is with the require_once(). Are you sure you have
      > arr.php on the root dir?[/color]

      And that the syntax for the array is wrong:

      1. you need the $ before the array name
      2. the array elements are specified with []

      so ...

      <?php
      $array[1] = 'ok1@Ok.com';
      ?>
      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Tormod Fjeldskår

        #4
        Re: how do you access an array in an included/required field?

        NotGiven wrote:
        [color=blue]
        > I have a large array that would be messy to include in the file "main.php"
        > itself and would prefer to include/require it. Say I have the following
        > line in "main.php":
        >
        > require_once('/arr.php');
        >
        > And the "arr.php" file looks something like this:
        >
        > <?php
        > array(1)='ok1@O k.com';
        > array(2)='ok2@O k.com';
        > array(3)='ok3@O k.com';
        > array(4)='ok4@O k.com';
        > array(5)='ok5@O k.com';
        > array(6)='ok6@O k.com';
        > array(7)='ok7@O k.com';
        > array(8)='ok8@O k.com';
        > array(9)='ok9@O k.com';
        > array(10)='ok10 @Ok.com';
        > ?>[/color]

        This is incorrect syntax. Try:

        <?php
        $array = array(
        1 => "ok1@Ok.com ",
        2 => "ok2@Ok.com ",
        3 => "ok3@Ok.com "
        );
        ?>
        [color=blue]
        > Would I be able to reference the array AS IF I had declared the array in
        > "main.php"?[/color]

        Yes. When you include/require the array, you can treat it as it is
        declared in the file it's included in. Thus, this is a valid main.php:

        <?php

        require_once('a rr.php');

        echo $array[1];

        ?>

        --
        Tormod Fjeldskår
        tormod@fritidsp roblemer.no

        Comment

        Working...