array scope???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lisawebs
Forum Commoner
Posts: 44
Joined: Wed Nov 19, 2003 6:21 pm

array scope???

Post by lisawebs »

I intend to build transaction handlers in one php program.
I have standard/common routines to read/write/select.
2 questions:

1. Do I have to declare as global all variables that will hold field-values
or, I can use an array (declared outside the function),
and declare it as global inside the function?

2. Do I have to use declarative field-names
or theres a way to store them as a reference into an array?
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

As no one has answered you I think you need to be a little more clearer with your question. I am not exactly sure what you are asking.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

As Paddy, I'm not entirely sure either of what you need to do...
So a shot in the dark to demonstrate some usage (if you didn't know allready that is ;)):

Code: Select all

<pre>
<?php
$array = array(
    'foo',
    'bar',
    'something' => 'here'
);
function x() {
    global $array;
    $array[] = 'moo'; // append moo using def. key incr.
    $array['G'] = 'lhama'; // ...and lhama, using named key.
}

print_r($array);
echo '<br>';
x();
print_r($array);
?>
Result:

Code: Select all

Array
(
    &#1111;0] =&gt; foo
    &#1111;1] =&gt; bar
    &#1111;something] =&gt; here
)

Array
(
    &#1111;0] =&gt; foo
    &#1111;1] =&gt; bar
    &#1111;something] =&gt; here
    &#1111;2] =&gt; moo
    &#1111;G] =&gt; lhama
)
lisawebs
Forum Commoner
Posts: 44
Joined: Wed Nov 19, 2003 6:21 pm

explanation

Post by lisawebs »

Can I do this? (of course syntax is uncomplete) example:

// this code is supposed to be fixed)
$fieldname1 = 'real_name' // of mysql table
...
$afields = array($fieldname1,...)
...
$value1 = "abc"
$avalues = array($value1,...)
...
$sql = "INSERT INTO `table` ($afields[1]) VALUES ($avalue[1])"

// now I can ask for values (on the screen)
// and the user types in $value1 "xyz"
// then call the function

dowrite($sql)


function dowrite($sql, $afields, $avalues){
// I don't want to pass the real names/values
// I want to pass the reference to them, so
// so when the string $sql is evaluated/executed
// program will take the values existing at that moment
// in this case "xyz"












dowrite
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

No changes, just making easier to read :D

Code: Select all

<?php
// this code is supposed to be fixed)
$fieldname1 = 'real_name' // of mysql table
...
$afields = array($fieldname1,...)
...
$value1 = "abc"
$avalues = array($value1,...)
...
$sql = "INSERT INTO `table` ($afields[1]) VALUES ($avalue[1])"

// now I can ask for values (on the screen)
// and the user types in $value1 "xyz"
// then call the function

dowrite($sql)


function dowrite($sql, $afields, $avalues){
// I don't want to pass the real names/values
// I want to pass the reference to them, so
// so when the string $sql is evaluated/executed
// program will take the values existing at that moment
// in this case "xyz"


?>
Post Reply