Search code examples
phpsyntaxescapingdouble-quotessingle-quotes

php if then else statement syntax error somewhere


So I am getting the following parse error when this file loads:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

I have tried everything to work out why and I narrowed it down to the

href=\"start.php?id=<?php echo $res['id'] ?>\"

section of the code. I am sure I left out a ' or " but unsure where as it all makes sense to me. Can anyone with a keener eye see where I am going wrong? Thank you.

My code:

<td>

<?php if($res['ndaSent'] == "No") {
echo "<span class=\"buttonTestDisabled\"> Start Test</span>";} 
else {
echo "<a class=\"buttonTest\" href=\"start.php?id=<?php echo $res['id'] ?>\">Start Test</a> ";}
?>

</td>

Solution

  •     echo "<a class=\"buttonTest\" href=\"start.php?id=<?php echo $res['id'] ?>\">Start Test</a> ";
    }
    

    Is incorrect, you are closing the PHP code before you are done with the PHP code. And $res['id'] cannot be in your echo like that, you should interpolate the variable correctly in the string. Remove the starting and closing tag inside the echo like this:

    echo "<a class=\"buttonTest\" href=\"start.php?id={$res['id']}\">Start Test</a> ";
    

    Please check this awesome guide on how to fix these kind of syntax errors.