我在HTML中的PHP代码
$i = 1;
echo "<input type='text' class='uborder help-80percent' maxlength='3' onkeypress='return isNumberKey(event)'' id='quantity_".$i."' onchange='updatetotalWeight(this.value, quantity_".$i.", current_quantity_".$i.", weight_".$i." )' />";
我想做我的:
updatetotalWeight(this.value, "quantity_1", "current_quanitity_1", "weight_1");
你知道如何让这个东西变成字符串吗?
答案 0 :(得分:3)
您可以在双引号字符串中使用双引号,方法是使用反斜杠\"
转义它们:
echo "<input type='text' class='uborder help-80percent' maxlength='3' onkeypress='return isNumberKey(event)' id='quantity_".$i."' onchange='updatetotalWeight(this.value, \"quantity_".$i."\", \"current_quantity_".$i."\", \"weight_".$i."\" )' />";
答案 1 :(得分:1)
您需要转义双引号\"
,同样,您不需要使用字符串连接.
,即:
$i = 1;
echo "<input type='text' class='uborder help-80percent' maxlength='3' onkeypress='return isNumberKey(event)'' id='quantity_$i' onchange='updatetotalWeight(this.value, \"quantity_$i\", \"current_quantity_$i\", \"weight_$i\" )' />";
答案 2 :(得分:0)
尝试使用双引号来翻看单引号并围绕字符串,这样你就可以获得插值的好处。
$var = "quotes";
echo "i hate \'$var\'"; // outputs "i hate quotes 'quotes'"
基本上尽量避免使用&#39;&#39; 。 &#39;&#39;因为它会导致很多混乱
祝你好运答案 3 :(得分:0)
或者,您可以使用HEREDOC来避免逃脱的所有需要。
<?Php
echo <<<HEREDOCNAME
The string starts here.
Literal second line of your output string. Your {$variable}. Or if you want to echo a {$_SESSION['variable']} or a {$_POST['variable']}.
This would be another literal end line with a literal tab. No need for backslash-n or backslash-t.
Need to echo a "double quote"? Or a 'single quote'?
Another end line and below is how to end the HEREDOC (note that the end line must not have any space before the heredoc name):
HEREDOCNAME;
// note that curly braces can also be used on:
echo "My variable is {$variable}."
// But never on single quotes.
echo 'Single quotes should always be like this: '.$variable;
// Nevertheless, you can also avoid concatenations and curly braces.
echo "This is my $variable";
// But things can get nasty if
echo "Your string and $variableasdsad are a mixed mess.";
// So
echo "always{$use}proper{$escaping}. Because thing\$cangetnastyjustlike$that."
?>