Conflict with Certain Symbols Inside Quotes. (example: $mystring = "<text here>"

I have a quiz created in PHP. This quiz will test a users knowledge of javascript. Unfortunately whenever I include javascript code in the questions this creates a conflict with the php code.

For example

$question1 = “1. Inside which HTML element do we put the JavaScript?”;
$question1_correct_answer ="a. ";
$question1_option2 ="b. ";
$question1_option3 ="c. ";
$question1_option4 ="d. ";
$_SESSION[‘question1’] = $question1;

$question2 = “2. How do you create a function?”;
$question2_correct_answer =“function:myFunction()”;
$question2_option2 =“function myFunction()”;
$question2_option3 =“function=myFunction()”;
$question2_option4 =“All are correct”;
$_SESSION[‘question2’] = $question2;

I know the solution is simple. Please help me here. Thank you

There should not be any conflict with PHP… do you mean conflict with HTML? When you’re outputting your questions/answers to a HTML page, you need to encode special chars i.e. < becomes < and > becomes > etc. There is a function in PHP that will do this: htmlspecialchars()
[php]<?php
$question1_correct_answer ="a. ";
echo htmlspecialchars($question1_correct_answer);
?>[/php]

Perhaps a conflict with HTML.

Thanks for trying but unfortunately your suggestion did not work. The text is not appearing at the correct location:

Please see:
http://www.mytestpapers.com/tests/javascript/quiz.php

I meant to provide this link: http://www.mytestpapers.com/tests/javascript/quiz.php

Sure. You need to output this code in the correct location. But “conflict” with HTML is resolved, right? :slight_smile:

It seems I may have got it working by using the following code:

[php]$question1 = htmlspecialchars(“1. Inside which HTML element do we put the JavaScript?”);
$question1_correct_answer = htmlspecialchars("");
// $question1_correct_answer = htmlspecialchars($question1_correct_answer);
$question1_option2 = htmlspecialchars("");
$question1_option3 = htmlspecialchars("");
$question1_option4 = htmlspecialchars("");[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service