0

How to use php to output this html code? The html code is this.

<a href="#false" onclick="load_page('piano_programs.php')">Piano Programs</a>

but I want to use php to show this code

$program_name = "piano_programs";
echo "<a href='#false' onclick="load_page('$program_name'.php)">Piano Programs</a>";

but.....doesn't work, any idea ,thanks

2
  • 4
    Step 1. Get an editor with syntax highlighting. Step 2. Learn basic PHP syntax. Commented Jun 1, 2017 at 2:07
  • As Enstage shows in their answer: if you have a string-literal and it has quotes inside of it, you need to escape them. This is because PHP can't tell if the quote (a) is a quote or (b) the end of the string-literal. Commented Jun 1, 2017 at 2:12

2 Answers 2

2

Backslashes to escape the quotes:

echo "<a href='#false' onclick=\"load_page('piano_programs.php')\">Piano Programs</a>";
Sign up to request clarification or add additional context in comments.

Comments

0

You need to add slashes before onclick's two double quotes and move your single quote to the right side of .php.

echo "<a href='#false' onclick=\"load_page('{$program_name}.php')\">Piano Programs</a>";

I like to curly-bracket my variables so that they stand out in my editor and so that they don't get mixed up with the text that immediately follows.

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.