Page 1 of 1
array scope???
Posted: Thu Dec 18, 2003 1:11 pm
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?
Posted: Thu Dec 18, 2003 3:39 pm
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.
Posted: Thu Dec 18, 2003 3:48 pm
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
(
ї0] => foo
ї1] => bar
їsomething] => here
)
Array
(
ї0] => foo
ї1] => bar
їsomething] => here
ї2] => moo
їG] => lhama
)
explanation
Posted: Fri Dec 19, 2003 11:22 am
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
Posted: Fri Dec 19, 2003 3:34 pm
by Derfel Cadarn
No changes, just making easier to read
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"
?>