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
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/>";
   }

}
Edited by Cornelius
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.