I've tried everything; and I can't seem to get past this VERY
(seemingly) simply problem.
I want to work with an array variable within a function(s).
I can't get it to work; if I:
1) global $arr=array(); (syntax err)
2) global $arr; in "main", the var isn't global
3) global $arr; in function, the array is cleared each time
I know I must be missing something REALLY simple/dumb; but just can't
seem to find it! any help appreciated!
tia - Bob
ei:
<?php
for ($x=1; $x<=2; $x++) {
print "\nloop # $x\n";
if($x==1) {
$row[1]="Why"; $row[2]="doesn't"; $row[3]="this work??";
$row[4]="this does"; }
if($x==2) {
$row[1]="This is"; $row[2]="the 2nd"; $row[3]="time thru.";
$row[4]="all done"; }
print "123before: 1:$arr[1] 2:$arr[2] 3:$arr[3] s:$s\n";
print "456before: 4:$arr[4] 5:$arr[5] 6:$arr[6] s:$s\n";
myf($x,$row);
print "123after : 1:$arr[1] 2:$arr[2] 3:$arr[3] s:$s\n";
print "456after : 4:$arr[4] 5:$arr[5] 6:$arr[6] s:$s\n";
}
function myf($x,$frow) {
global $arr, $s;
if ($x==1) { $arr=array(1=>$ frow[1],2=>$frow[2],3=>$frow[3]);
$s=$frow[4]; }
if ($x==2) { $arr=array(4=>$ frow[1],5=>$frow[2],6=>$frow[3]);
$s=$frow[4]; }
print "123during: 1:$arr[1] 2:$arr[2] 3:$arr[3] s:$s\n";
print "456during: 4:$arr[4] 5:$arr[5] 6:$arr[6] s:$s\n";
}
?>
(seemingly) simply problem.
I want to work with an array variable within a function(s).
I can't get it to work; if I:
1) global $arr=array(); (syntax err)
2) global $arr; in "main", the var isn't global
3) global $arr; in function, the array is cleared each time
I know I must be missing something REALLY simple/dumb; but just can't
seem to find it! any help appreciated!
tia - Bob
ei:
<?php
for ($x=1; $x<=2; $x++) {
print "\nloop # $x\n";
if($x==1) {
$row[1]="Why"; $row[2]="doesn't"; $row[3]="this work??";
$row[4]="this does"; }
if($x==2) {
$row[1]="This is"; $row[2]="the 2nd"; $row[3]="time thru.";
$row[4]="all done"; }
print "123before: 1:$arr[1] 2:$arr[2] 3:$arr[3] s:$s\n";
print "456before: 4:$arr[4] 5:$arr[5] 6:$arr[6] s:$s\n";
myf($x,$row);
print "123after : 1:$arr[1] 2:$arr[2] 3:$arr[3] s:$s\n";
print "456after : 4:$arr[4] 5:$arr[5] 6:$arr[6] s:$s\n";
}
function myf($x,$frow) {
global $arr, $s;
if ($x==1) { $arr=array(1=>$ frow[1],2=>$frow[2],3=>$frow[3]);
$s=$frow[4]; }
if ($x==2) { $arr=array(4=>$ frow[1],5=>$frow[2],6=>$frow[3]);
$s=$frow[4]; }
print "123during: 1:$arr[1] 2:$arr[2] 3:$arr[3] s:$s\n";
print "456during: 4:$arr[4] 5:$arr[5] 6:$arr[6] s:$s\n";
}
?>
Comment