I'm trying to echo a functional button in php.
echo "<button type='button' onclick="alert('Hello world!')" >Buy</button>";
Change your code to this. Currently you have double speech marks within the code you want echoed. This means it is being mistaken for the end of the code in the middle of your echo statement. Putting backslashes in front of these escapes them so that they are are not interpreted as being part of the PHP syntax.
echo "<button type='button' onclick='alert(\"Hello world!\")' >Buy</button>";