-1

I face a problem about the following code.

<?php 
$sql="SELECT * FROM `question` WHERE sub_id=$subject_id";
$query=mysql_query($sql);
$question_count=mysql_num_rows($query);
for($a=1;$a<=$question_count;$a++){
    $ans{$a}=$_POST["ans$a"];
    $subject_id{$a}=$_POST["sub_id$a"];
    $group_id{$a}=$_POST["grup_id$a"];  
}

?>

I want to use $ans{$a}, $subject_id{$a}, $subject_id{$a} etc variable outside for loop. If I use those like following code,

<?php 
$sql="SELECT * FROM `question` WHERE sub_id=$subject_id";
$query=mysql_query($sql);
$question_count=mysql_num_rows($query);
for($a=1;$a<=$question_count;$a++){
    $ans{$a}=$_POST["ans$a"];
    $subject_id{$a}=$_POST["sub_id$a"];
    $group_id{$a}=$_POST["grup_id$a"];
}

    echo $ans1;
    echo $subject_id1;
?>  

php says that:

Notice: Undefined variable: ans1 in F:\xampp\htdocs\oes\Student\result_process.php on line 25.

please help me give some advice to use these variable outside for loop scope.

3
  • I haven't used such constructs as I think it is bad practice to use them. But shouldn't it be ${"ans".$a} instead of $ans{$a} Commented Jun 15, 2014 at 14:39
  • In your loop, do you mean to write: ${'ans' . $a}? In other words dynamically creating a variable, such as $ans1? Commented Jun 15, 2014 at 14:40
  • yes i want it. that's variable dynamically create $ans1. i will try your suggestion. Commented Jun 15, 2014 at 14:50

2 Answers 2

0

The problem with your code is that $ans{$a} is equal to $ans[$a] (PHP curly braces in array notation)

So your loop does this:

 for($a=1;$a<=$question_count;$a++){
     $ans[$a]=$_POST["ans$a"];
     $subject_id[$a]=$_POST["sub_id$a"];
     $group_id[$a]=$_POST["grup_id$a"];  
 }

That's why $ans1 is not defined. If you would write echo $ans[1] you would get the result.

If you really want a variable called $ans1 you would need to write it that way:

 for( $a = 1 ; $a <= $question_count ; $a++ ){
     ${"ans" . $a} = $_POST["ans$a"];
     ${"subject_id" . $a} = $_POST["sub_id$a"];
     ${"group_id" . $a} = $_POST["grup_id$a"];  
 }

But as I wrote in my comment I think this way of writing code should be avoided.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry this code also show same problem. still now it say. Notice: Undefined variable: ans3 in F:\xampp\htdocs\oes\Student\result_process.php on line 65 i want to use ans3 variable. it work if i use such like this variable ${"ans" . $a} instead of $ans[$a]
@Sifat080 If you can access $ans1 but $ans3 would result in an Notice: Undefined variable: ans3 then $question_count is smaller then 3. What do you want to say with it work if i use such like this variable ${"ans" . $a} instead of $ans[$a] ?
0

Possible variable $question_count can be 0. Otherwise try to use arrays:

<?php 
$sql="SELECT * FROM `question` WHERE sub_id=$subject_id";
$query=mysql_query($sql);
$question_count=mysql_num_rows($query);
$items = array();

for($a=1;$a<=$question_count;$a++){
    $items[$a] = array(
       'ans' => $_POST["ans$a"],
       'subject_id' => $_POST["sub_id$a"],
       'group_id' => $_POST["grup_id$a"]
    );
}

echo $items[1]['ans'];
echo $items[1]['subject_id'];
?> 

1 Comment

Even if $question_count would never be 0, $ans1 would never be set with the code given by the OP as $ans{$a} is equal to $ans[$a].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.