I have this line of code below:
foreach ($searchResults as $key=>$question) {
echo '<tr class="questiontd"><td>'.htmlspecialchars($question).'</td>';
echo '<td class="optiontypetd">'.htmlspecialchars($searchOption[$key]).'</td>';
echo '<td class="noofanswerstd">'.htmlspecialchars($searchNoofAnswers[$key]).'</td>';
echo '<td class="answertd">'.htmlspecialchars($searchAnswer[$key]).'</td>';
echo '<td class="noofrepliestd">'.htmlspecialchars($searchReply[$key]).'</td>';
echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>';
echo "<td class='addtd'><button type='button' class='add' onclick=\"parent.addwindow('$question','$searchMarks[$key]','$searchNoofAnswers[$key]','$searchOption[$key]','$searchReply[$key]','$searchAnswer[$key]');\">Add</button></td></tr>";
}
Now I wanted to use str_repace to replace some string characters but when I do this I get this error in the line above:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in
…text.php on line 154
How can I remove this error?
UPDATE:
OK I remove the single quote as mentioned but problem is that it messes my add button up. I am now getting this error when I click on the “Add” button:
syntaxError: unterminated string literal error:
parent.addwindow(‘!
The line of code where the error is displayed like this in the console:
<button type='button' class='add' onclick="parent.addwindow('!"�$%^&*()-=\'.,:;/?#~*/\\><'
How can this be fixed?
T_CONSTANT_ENCAPSED_STRINGis basically a quote mismatch. StackOverflow’s syntax highlighter makes it more obvious with the color change.Remove the single quote and you should be good.
Follow-up:
Alternatives:
As Cups mentioned, you can use
heredoc(though I personally think it doesn’t help in this particular scenario from a readability standpoint–you’re still embedding a lot of variables within HTML syntax). Another method is to store theparent.addwindowarguments as a variable then pass it within the string (save yourself from both an extended echo line and worrying about quote mis-matches. For example:Alternatively, you can use
sprintfinstead ofimplodewith the same echo call:This gives you more control in case some arguments are numbers, some are strings, etc.