0

Consider the following code for printing questions from text file:

    foreach ($lines as $line_num => $line) {
        if($line_num%3 == 1){
            echo 'Question '.$count.':'.'<br/>'.'<input type="text" value="$line" class="tcs"/>'.'<br/>';

I've tried many string escaping combinations. The problem is that I get $line inside the text field instead of the variable value. Any help is greatly appreciated.

1
  • Are you using PHP first time? Commented Feb 14, 2012 at 19:25

3 Answers 3

3

Remove the variable from the ' quoted string, or use " so the variable is interpreted.

echo 'Question ' . $count . ':<br/><input type="text" value="' . $line . '" class="tcs"/><br/>';

or

echo "Question " . $count . ":<br/><input type=\"text\" value=\"$line\" class=\"tcs\"/><br/>";

The first option is better, since you don't have to escape anything else.

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

Comments

2

Did you try:

echo 'Question ' . $count . ':'.'<br/>'.'<input type="text" value="' . $line . '" class="tcs"/>'.'<br/>';

Comments

2

Variables don't get processed in single quoted strings. You need to use double quotes or another way of inserting them (such as concatenation).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.