Jaswinder Posted April 24, 2014 Share Posted April 24, 2014 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 More sharing options...
Ch0cu3r Posted April 24, 2014 Share Posted April 24, 2014 What? Your code makes no sense. What is it you're trying to do? Link to comment https://forums.phpfreaks.com/topic/287987-using-declared-array-in-a-function/#findComment-1477155 Share on other sites More sharing options...
Jaswinder Posted April 24, 2014 Author Share Posted April 24, 2014 it properly makes sense.. i just want to use the array $answer inside a function.. how can i use it ? Link to comment https://forums.phpfreaks.com/topic/287987-using-declared-array-in-a-function/#findComment-1477159 Share on other sites More sharing options...
Ch0cu3r Posted April 24, 2014 Share Posted April 24, 2014 By passing it as an argument when it is called $val = 'some value'; $answer = array('val1', 'val2'); numcheck($val, $answer); Link to comment https://forums.phpfreaks.com/topic/287987-using-declared-array-in-a-function/#findComment-1477160 Share on other sites More sharing options...
Cornelius Posted April 24, 2014 Share Posted April 24, 2014 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/>"; } } Link to comment https://forums.phpfreaks.com/topic/287987-using-declared-array-in-a-function/#findComment-1477163 Share on other sites More sharing options...
Cornelius Posted April 24, 2014 Share Posted April 24, 2014 You also could have used $_POST array directly in the second file: $val=10; numcheck($val); function numcheck($sent_val){ echo $sent_val . "<br/>"; foreach ($_POST as $mark){ echo $mark . "<br/>"; } } Link to comment https://forums.phpfreaks.com/topic/287987-using-declared-array-in-a-function/#findComment-1477165 Share on other sites More sharing options...
Jaswinder Posted April 24, 2014 Author Share Posted April 24, 2014 Thanx for reply guys.. i got my problem solved by some other method... your answers help me in doing that Link to comment https://forums.phpfreaks.com/topic/287987-using-declared-array-in-a-function/#findComment-1477197 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.