By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 IT Pros & Developers. It's quick & easy.

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

P: n/a
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)='***@Ok.com';
array(2)='***@Ok.com';
array(3)='***@Ok.com';
array(4)='***@Ok.com';
array(5)='***@Ok.com';
array(6)='***@Ok.com';
array(7)='***@Ok.com';
array(8)='***@Ok.com';
array(9)='***@Ok.com';
array(10)='****@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.
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
NotGiven wrote:
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)='***@Ok.com'; [snipped] ?>

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.


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 =--
Jul 17 '05 #2

P: n/a
Pedro Graca wrote:
NotGiven wrote:
<?php
array(1)='***@Ok.com';
I guess your problem is with the require_once(). Are you sure you have
arr.php on the root dir?


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] = 'o**@Ok.com';
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #3

P: n/a
NotGiven wrote:
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)='***@Ok.com';
array(2)='***@Ok.com';
array(3)='***@Ok.com';
array(4)='***@Ok.com';
array(5)='***@Ok.com';
array(6)='***@Ok.com';
array(7)='***@Ok.com';
array(8)='***@Ok.com';
array(9)='***@Ok.com';
array(10)='****@Ok.com';
?>
This is incorrect syntax. Try:

<?php
$array = array(
1 => "ok*@Ok.com",
2 => "ok*@Ok.com",
3 => "ok*@Ok.com"
);
?>
Would I be able to reference the array AS IF I had declared the array in
"main.php"?


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('arr.php');

echo $array[1];

?>

--
Tormod Fjeldskår
to****@fritidsproblemer.no
http://tormod.fritidsproblemer.no/
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.