How to change a \n value for a value like X

You can do that like this:

   for($i=1; $i <= 5; $i++) {
        $answer= ${'q' . $i};
        if($answer=='') $answer='X';        
    }

Edit: If you just only need to update value, you can do that like this,

   for($i=1; $i <= 5; $i++) {
        if(${'q' . $i}=='') ${'q' . $i}='X';        
    }

If you want to change any of the $q* variables to 'X' if they are empty, you can use PHPs variable variables to do that. For example:

$q1 = '';
$q2 = 'A';
$q3 = '';
$q4 = 'E';
$q5 = 'D';
for ($i=1; $i <= 5; $i++) {
    if (${"q$i"} == '') ${"q$i"} = 'X';        
}
echo "$q1\n";
echo "$q2\n";
echo "$q3\n";
echo "$q4\n";
echo "$q5\n";

Output:

X
A
X
E
D

Demo on 3v4l.org

Tags:

Php