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

Which way is better ?

P: n/a
All,
Which way below is the better method ? Basically A.PHP calls a function from
B.PHP that contains a mess of db variables (i.e. fields, table names, etc.).
The variables that are returned are used throughout the rest of the A.PHP
script.
I have left out the contents of $a,$b and $c and changed the function names
and variable names for a more simple posting.

a.php
-----
db_function(); //from b.php

b.php
------
function db_function ()
{
global $a, $b, $c;
$a = ""; //fields
$b = ""; //table
$c = ""; //id
}

or this way...

a.php
-----
list ($a, $b, $c) = db_function();

b.php
------
function db_function ()
{
$a = ""; //fields
$b = ""; //table
$c = ""; //id
return array ($a, $b, $c);
}

Many thanks.
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sat, 3 Apr 2004 13:08:09 -0900, "StinkFinger" <st****@pinky.com> wrote:
Which way below is the better method ? Basically A.PHP calls a function from
B.PHP that contains a mess of db variables (i.e. fields, table names, etc.).
The variables that are returned are used throughout the rest of the A.PHP
script.
I have left out the contents of $a,$b and $c and changed the function names
and variable names for a more simple posting.

a.php
-----
db_function(); //from b.php

b.php
------
function db_function ()
{
global $a, $b, $c;
$a = ""; //fields
$b = ""; //table
$c = ""; //id
}

or this way...

a.php
-----
list ($a, $b, $c) = db_function();

b.php
------
function db_function ()
{
$a = ""; //fields
$b = ""; //table
$c = ""; //id
return array ($a, $b, $c);
}


The general rule is avoid globals unless you have a good reason for it. So the
second one, probably.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #2

P: n/a
Thanks - I was putting some new found knowledge to work (i.e. from PHP
Visual Blueprint) and the first way was my original way, the second, ideas
taken from the book.

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:ar********************************@4ax.com...
On Sat, 3 Apr 2004 13:08:09 -0900, "StinkFinger" <st****@pinky.com> wrote:
Which way below is the better method ? Basically A.PHP calls a function
from
B.PHP that contains a mess of db variables (i.e. fields, table names,
etc.).
The variables that are returned are used throughout the rest of the A.PHP
script.
I have left out the contents of $a,$b and $c and changed the function
names
and variable names for a more simple posting.

a.php
-----
db_function(); //from b.php

b.php
------
function db_function ()
{
global $a, $b, $c;
$a = ""; //fields
$b = ""; //table
$c = ""; //id
}

or this way...

a.php
-----
list ($a, $b, $c) = db_function();

b.php
------
function db_function ()
{
$a = ""; //fields
$b = ""; //table
$c = ""; //id
return array ($a, $b, $c);
}


The general rule is avoid globals unless you have a good reason for it. So
the
second one, probably.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

Jul 17 '05 #3

P: n/a
Regarding this well-known quote, often attributed to StinkFinger's famous
"Sat, 3 Apr 2004 13:08:09 -0900" speech:
All,
Which way below is the better method ? Basically A.PHP calls a function from
B.PHP that contains a mess of db variables (i.e. fields, table names, etc.).
The variables that are returned are used throughout the rest of the A.PHP
script.
I have left out the contents of $a,$b and $c and changed the function names
and variable names for a more simple posting.

a.php
-----
db_function(); //from b.php

b.php
------
function db_function ()
{
global $a, $b, $c;
$a = ""; //fields
$b = ""; //table
$c = ""; //id
}

or this way...

a.php
-----
list ($a, $b, $c) = db_function();

b.php
------
function db_function ()
{
$a = ""; //fields
$b = ""; //table
$c = ""; //id
return array ($a, $b, $c);
}

Many thanks.


I'd recommend using a class. OOP would apply very well to this.

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.