在 html 中追加时连接字符串和 php 变量
Concat String and php Variable when appending in html
我使用了以下代码:
$html = '';
$html. = '<br></br>';
$html .= '<a href= "javascript:void(0)" class= "test" onclick= "get_data($access_token,1,$marketTagId);">Next 50 </a>';
但我的 PHP 变量无法识别。
双引号和单引号的区别
根据this Whosebug answer:
- Single quoted strings 将几乎完全显示内容 "as is." 变量和大多数转义序列将不会被解释。例外情况是要显示文字单引号,可以用反斜杠转义
\'
,要显示反斜杠,可以用另一个反斜杠转义 \
(所以是的,即使是单引号字符串也会被解析).
- Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable
$type
and you what to echo "The $types are"
That will look for the variable $types
. To get around this use echo "The {$type}s are"
You can put the left brace before or after the dollar sign. Take a look at string parsing 了解如何使用数组变量等。
解决方案
无法识别它们,因为您已将所有字符串放在单引号中 ('
)。
您应将它们全部替换为双引号 ("
)。
$html = "";
$html. = "<br></br>";
$html .= "<a href=\"javascript:void(0)\" class=\"test\" onclick=\"get_data('$access_token',1,'$marketTagId');\">Next 50</a>";
另一个解决方案
您还可以选择将变量与文本分开:
$html = '';
$html. = '<br></br>';
$html .= '<a href="javascript:void(0)" class="test" onclick="get_data("' . $access_token . '",1,"' . $marketTagId . '");">Next 50</a>';
另一种解决方案
如果不想转义这么多双引号,可以用单引号代替:
$html = "";
$html. = "<br></br>";
$html .= "<a href='javascript:void(0)' class='test' onclick='get_data(\"$access_token\",1,\"$marketTagId\");'>Next 50</a>";
试试这个,它可能对你有帮助
<?php
$html = '';
$html. = '<br></br>';
$html .= '<a href= "javascript:void(0)" class= "test" onclick= "get_data($access_token,1,'.$marketTagId.');">Next 50</a>';
?>
试试这个
get_data('".$access_token."',1,'".$marketTagId."');
或
get_data(<?php $access_token ?>,1,<?php $marketTagId ?> );
您需要围绕 PHP 变量编写 ''
否则在 onclick
函数中它们将不起作用。
使用下面的代码:-
$html = '';
$html. = '<br></br>';
$html .= "<a href= 'javascript:void(0)' class= 'test' onclick= \"get_data('$access_token','1','$marketTagId');\">Next 50 </a>";
希望对您有所帮助:)
在 {
和 }
中使用 php 变量
$access_token = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$marketTagId = "__AAA__B_123";
$html = "";
$html .= "<br></br>";
$html .= "<a href=\"javascript:void(0)\" class=\"test\" onclick=\"get_data('{$access_token}',1, '{$marketTagId}')\">Next 50</a>";
echo htmlentities($html);
您的代码:
$html. = '<br></br>';
$html .= '<a href= "
更改为:
$html.= '<br></br>';
$html.= '<a href= "
试试这个:
$html = '';
$html.= '<br></br>';
$html.= '<a href= "javascript:void(0)" class= "test" onclick= "get_data($access_token,1,$marketTagId);">Next 50 </a>';
你能期待这个吗:
大家好感谢您的帮助。
这对我有用:
$html .= '</table>';
$html .= '<br><br>';
$html .= '<a href= "javascript:void(0)" class= "test" onclick= \'get_data("'.$access_token.'","1","'.$marketTagId.'");\'>Next 50 </a>';
我使用了以下代码:
$html = '';
$html. = '<br></br>';
$html .= '<a href= "javascript:void(0)" class= "test" onclick= "get_data($access_token,1,$marketTagId);">Next 50 </a>';
但我的 PHP 变量无法识别。
双引号和单引号的区别
根据this Whosebug answer:
- Single quoted strings 将几乎完全显示内容 "as is." 变量和大多数转义序列将不会被解释。例外情况是要显示文字单引号,可以用反斜杠转义
\'
,要显示反斜杠,可以用另一个反斜杠转义\
(所以是的,即使是单引号字符串也会被解析). - Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable
$type
and you what toecho "The $types are"
That will look for the variable$types
. To get around this useecho "The {$type}s are"
You can put the left brace before or after the dollar sign. Take a look at string parsing 了解如何使用数组变量等。
解决方案
无法识别它们,因为您已将所有字符串放在单引号中 ('
)。
您应将它们全部替换为双引号 ("
)。
$html = "";
$html. = "<br></br>";
$html .= "<a href=\"javascript:void(0)\" class=\"test\" onclick=\"get_data('$access_token',1,'$marketTagId');\">Next 50</a>";
另一个解决方案
您还可以选择将变量与文本分开:
$html = '';
$html. = '<br></br>';
$html .= '<a href="javascript:void(0)" class="test" onclick="get_data("' . $access_token . '",1,"' . $marketTagId . '");">Next 50</a>';
另一种解决方案
如果不想转义这么多双引号,可以用单引号代替:
$html = "";
$html. = "<br></br>";
$html .= "<a href='javascript:void(0)' class='test' onclick='get_data(\"$access_token\",1,\"$marketTagId\");'>Next 50</a>";
试试这个,它可能对你有帮助
<?php
$html = '';
$html. = '<br></br>';
$html .= '<a href= "javascript:void(0)" class= "test" onclick= "get_data($access_token,1,'.$marketTagId.');">Next 50</a>';
?>
试试这个
get_data('".$access_token."',1,'".$marketTagId."');
或
get_data(<?php $access_token ?>,1,<?php $marketTagId ?> );
您需要围绕 PHP 变量编写 ''
否则在 onclick
函数中它们将不起作用。
使用下面的代码:-
$html = '';
$html. = '<br></br>';
$html .= "<a href= 'javascript:void(0)' class= 'test' onclick= \"get_data('$access_token','1','$marketTagId');\">Next 50 </a>";
希望对您有所帮助:)
在 {
和 }
$access_token = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$marketTagId = "__AAA__B_123";
$html = "";
$html .= "<br></br>";
$html .= "<a href=\"javascript:void(0)\" class=\"test\" onclick=\"get_data('{$access_token}',1, '{$marketTagId}')\">Next 50</a>";
echo htmlentities($html);
您的代码:
$html. = '<br></br>';
$html .= '<a href= "
更改为:
$html.= '<br></br>';
$html.= '<a href= "
试试这个:
$html = '';
$html.= '<br></br>';
$html.= '<a href= "javascript:void(0)" class= "test" onclick= "get_data($access_token,1,$marketTagId);">Next 50 </a>';
你能期待这个吗:
大家好感谢您的帮助。 这对我有用:
$html .= '</table>';
$html .= '<br><br>';
$html .= '<a href= "javascript:void(0)" class= "test" onclick= \'get_data("'.$access_token.'","1","'.$marketTagId.'");\'>Next 50 </a>';