PHP:回显字符串中的单引号和双引号问题


PHP: single and double quotes issue in echo-ed string?

我有一个非常简单的问题,我不知道如何解决。

基本上,我有这个字符串:
$variable = '<input type="button" value="something" onclick="location.href = 'my-page.php';" />';

问题是按钮上的onclick="..."

当我运行我的代码时,它会杀死整个页面,没有任何错误。

谁能告诉我是否有办法解决这个问题?

您需要转义撇号:

$variable = '<input ... onclick="location.href = ''my-page.php'';" />';