Jump to content

using declared array in a function


Jaswinder

Recommended Posts

hi everyone.

i want to use an array ( already created outside the function ) in a function .

here is the code

page1.php

<form action="page2.php" method="post">
Marks1 = <input type="text" name="marks1" />
Marks2 = <input type="text" name="marks2" />
<input type="submit">

page2.php

extract($_POST);

$answer=array("$marks1","$marks2");

function numcheck($val,$answer)
{
	if(is_numeric($val))
	{
        return $val;
         }
	else
	{
	$newanswer=serialize($answer);
	$encode=urlencode($newanswer);
	header('location:level1test?num=numerror&encode=$encode');
	}
}

$result=array_filter($answer,"numcheck");

i am getting this error :- Missing argument 2 for numcheck()

 

i tried to use array inside the function also like this


function numcheck($val,$answer)
{
	if(is_numeric($val))
	{
        return $val;
         }
	else
	{

        $answer=array("$marks1","$marks2");

	$newanswer=serialize($answer);
	$encode=urlencode($newanswer);
	header('location:level1test?num=numerror&encode=$encode');
	}
}

But now the error is undefined varible marks1 and marks2

 

 

How to get the $answer array inside the function ??

Link to comment
https://forums.phpfreaks.com/topic/287987-using-declared-array-in-a-function/
Share on other sites

You first need to add missing tags in page1.php

<html>
   <body>
     <form action="page2.php" method="post">
                Marks1 = <input type="text" name="marks1" /> 
                Marks2 = <input type="text" name="marks2" />
               <input type="submit">
        </form> 
    </body>
</html>
You're passing array to page2.php and extracting it to separate variables, and then again making a new array from those variables. Nevertheless you can use it no problem:

 

 

extract($_POST);

$val=10;
$answer=array( $marks1, $marks2);
numcheck($val, $answer);


function numcheck($sent_val,$sent_answer){
   echo $sent_val . "<br/>";
   foreach ($sent_answer as $mark){
          echo $mark . "<br/>";
   }

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.