Need to use a numeric array to receive data into a function.

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
Steve1951
Forum Newbie
Posts: 2
Joined: Thu Mar 26, 2009 12:35 pm

Need to use a numeric array to receive data into a function.

Post by Steve1951 »

I'm new to PHP programming. I need to use a numeric array to receive data into a function but when I do I receive the following error:

PHP Parse error: parse error, unexpected '[', expecting ')' in G:\\...\\test_array.php on line 47

In the sample code below, test1 and test2 function properly but test3 fails with the above error. The reason I want to use an array is I need to cycle through 14 to 20 parameters passed to the function.

Any help will be appreciated (either with the array or an alternate method to cycle through the parameters in a for loop).

Thanks,
Steve

Code: Select all

<html>
<head>
  <title>PHP Test Array in a function</title>
</head>
<body>
<h1>PHP Test Array in a function</h1>
 
<?php
 
// TEST 1
// Assign data into a numeric array and output to the screen.
echo "Start of test 1<br />";
 
$sch[1] = "Sched one";
$sch[2] = "Sched two";
$sch[3] = "Sched three";
$sch[4] = "Sched four";
 
  for ($ctr=1; $ctr<=3; $ctr++)
  {
    echo "Counter $ctr $sch[$ctr]<br />";
  }
 
echo "End of test 1<br /><br />";
 
// TEST 2
// Use a function to assign data into variables and then move the data from variables into a numeric array.
function putSchedMat($sch1, $sch2, $sch3, $sch4) {
  echo "Start of test 2<br />";
 
  $sch[1] = $sch1;
  $sch[2] = $sch2;
  $sch[3] = $sch3;
  $sch[4] = $sch4;
 
  for ($ctr=1; $ctr<=3; $ctr++)
  {
    echo "Counter $ctr $sch[$ctr]<br />";
  }
 
echo "End of test 2<br /><br />";
 
}
 
// TEST 3
// Use a function to assign data into a numeric array.
function putSchedMat2($sch[1], $sch[2], $sch[3], $sch[4]) {
  echo "Start of test 3<br />";
 
  for ($ctr=1; $ctr<=3; $ctr++)
  {
    echo "Counter $ctr $sch[$ctr]<br />";
  }
 
echo "End of test 3<br /><br />";
 
}
 
// Call the putSchedMat function.
putSchedMat("This is one", "This is two", "This is three", "This is four");
 
// Call the putSchedMat2 function.
//putSchedMat2("This is first", "This is second", "This is third", "This is fourth");
 
 
?>
 
<!--[Thu Mar 26 09:52:12 2009] [error] [client 127.0.0.1] PHP Parse error:  parse error, unexpected '[', expecting ')' in G:\\...\\test_array.php on line 47-->
 
</body>
</html>
 
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Need to use a numeric array to receive data into a function.

Post by Mark Baker »

Code: Select all

 
function putSchedMat() {
    $parameters = func_get_args();
    foreach($parameters as $parameter)  {
        if (is_array($parameter)) {
            print_r($parameter);
            echo '<br />';
        } else {
            echo $parameter.'<br />';
        }
    }
}
 
putSchedMat(1,2,3,4,5);
echo '<hr />';
putSchedMat();
echo '<hr />';
putSchedMat('ABC', array(1,2,3,4),False);
echo '<hr />';
putSchedMat(1);
 
Steve1951
Forum Newbie
Posts: 2
Joined: Thu Mar 26, 2009 12:35 pm

Re: Need to use a numeric array to receive data into a function.

Post by Steve1951 »

Thanks - That's exactly what I needed.

Steve
Post Reply