This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Dreamweaver > June 2004 > Are arrays better than includes?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Are arrays better than includes?
David B

2004-06-10, 7:14 am

I have a large PHP include-driven website in the works. It's connected
to a database with information on the world's nations, states, etc.

For weeks, many of the questions I've posted on online forums and
newsgroups have been greeted by suggestions that I use an array system.
I made a few feeble attempts but never quite figured arrays out, and I
was too busy learning other things anyway.

Now I'm ready, so I went back to square one and asked some questions
about getting started with an array. Suddenly, a couple people who claim
to be PHP gurus told me I don't need an array - includes are just fine!

So what's the advantage of one over the other? I thought that, with an
array, you could enter one variable on a webpage, and it would drag
every variable related to it along.

For example, consider the following code from one of my pages:

<?php
$mynation = 'France';
$mynacode = 'fra';
$mycontient = 'Eurasia';
$continentcode = 'eur';
?>

If I used an array, I thought that all I had to do was post one of these
values - like $mycode = 'fra' - and any element in the body that echoed
$mynation would automatically insert France. Or if I inserted $mycode =
'ken';, then the page would automatically display Kenya and Africa in
place of $mynation and $mycontinent.

So what good are arrays if includes are just as good???
Gary White

2004-06-10, 12:14 pm

On Thu, 10 Jun 2004 00:22:46 -0700, David B
<david_blomstromDELETETHIS@yahoo.com> wrote:

>Now I'm ready, so I went back to square one and asked some questions
>about getting started with an array. Suddenly, a couple people who claim
>to be PHP gurus told me I don't need an array - includes are just fine!


I'm not a PHP guru, but I can tell you that both arrays and includes are
valuable tools, but they serve entirely different functions. I don't
understand, from the example you posted, what you're trying to
accomplish. Perhaps, if you explain a bit more, someone can offer a
suggestion.


Gary
Michael Fesser

2004-06-10, 7:14 pm

.oO(David B)

>Now I'm ready, so I went back to square one and asked some questions
>about getting started with an array. Suddenly, a couple people who claim
>to be PHP gurus told me I don't need an array - includes are just fine!


Includes and arrays are completely different things, they have nothing
to do with each other.

>So what's the advantage of one over the other? I thought that, with an
>array, you could enter one variable on a webpage, and it would drag
>every variable related to it along.


Nope. An array is just a container for variables, there are no such
automatismns.

>For example, consider the following code from one of my pages:
>
><?php
>$mynation = 'France';
>$mynacode = 'fra';
>$mycontient = 'Eurasia';
>$continentcode = 'eur';
>?>
>
>If I used an array, [...]


.... it could look like this for example (associative array):

$data = array(
'nation' => 'France',
'ncode' => 'fra''
'continent' => 'Eurasia'
'ccode' => 'eur'
);

Or (2-dimensional):

$data = array(
'nation' => array('France', 'fra'),
'continent' => array('Eurasia', 'eur')
);

Or (2-dimensional, completely associative):

$data = array(
'nation' => array('name' => 'France', 'code' => 'fra'),
'continent' => array('name' => 'Eurasia', 'code' => 'eur')
);

(just showing some different ways how arrays can be used)

But whatever the array looks like - the data has to come from somewhere.

>I thought that all I had to do was post one of these
>values - like $mycode = 'fra' - and any element in the body that echoed
>$mynation would automatically insert France.


Nope. In the code above you would have to use something like

print $data['nation']
print $data['ncode']

or

print $data['nation'][0]
print $data['nation'][1]

or

print $data['nation']['name']
print $data['nation']['code']

to print out the nation name and code. This code would look the same all
over the page, but you have to fill the array first with some values.
It's not enough to just assign the nation code for example, how should
PHP know where to get the rest of the data from?

>Or if I inserted $mycode =
>'ken';, then the page would automatically display Kenya and Africa in
>place of $mynation and $mycontinent.


That's not how it works.

>So what good are arrays if includes are just as good???


Depends. ;)

Micha
Sponsored Links


Copyright 2003 - 2009 forum4designers.com  Software forum  Computer Hardware reviews