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

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
francoleung
  • 237
  • 1
  • 7
  • 19
  • 4
    Step 1. Get an editor with syntax highlighting. Step 2. Learn basic PHP syntax. – Mike Jun 01 '17 at 02: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. – BrandonFlynn-NB Jun 01 '17 at 02:12

2 Answers2

2

Backslashes to escape the quotes:

echo "<a href='#false' onclick=\"load_page('piano_programs.php')\">Piano Programs</a>";
Enstage
  • 2,106
  • 13
  • 20
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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136