PHP解析错误!
我试图找出这个错误的原因(我试图使用PHP输出一些HTML) 我不知道解析错误在哪里:(。试试这个PHP解析错误!,php,string,text,escaping,Php,String,Text,Escaping,我试图找出这个错误的原因(我试图使用PHP输出一些HTML) 我不知道解析错误在哪里:(。试试这个 $newLI = "<li id = $row['id'] style='padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;'>"; $newLI=“”; 您需要将$row['id']用大括号括起来,如{$row['id']} 它不需要其他转义,尽管
$newLI = "<li id = $row['id'] style='padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;'>";
$newLI=“”;
您需要将$row['id']用大括号括起来,如{$row['id']}
它不需要其他转义,尽管它应该用单引号括起来,用于HTML属性:id='{$row['id']}'
因此,使用无字符串扭曲和双引号的完整答案是:
$newLI = "<li id='{$row['id']}' style='padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;'>";
$newLI=“”;
使用单引号和字符串串联可能更容易避免这些错误:
$newLI = '<li id="'.$row['id'].'" style="padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;">';
$newLI=';
此外,您不必转义HTML代码的双引号。实际答案将是:-
$newLI = '<li id = '.$row['id'].' style="padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;">';
$newLI=';
希望能有所帮助。改变
$newLI = "<li id = $row['\"id\"'] style=\"padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;\">"
$newLI=“”
致:
$rowID=$row['id'];
$newLI=“”
这是因为您引用了$row['id']。为什么不保持它的整洁和简单,如:
<?php
$newLI = "<li id =".$row['id']." style=\"padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;\">";
?>
您的问题在于$row本身
// The following is okay, as it's inside a string. Constants are not looked for
// within strings, so no E_NOTICE occurs here
print "Hello $arr[fruit]"; // Hello apple
// With one exception: braces surrounding arrays within strings allows constants
/ / to be interpreted
print "Hello {$arr[fruit]}"; // Hello carrot
print "Hello {$arr['fruit']}"; // Hello apple
// This will not work, and will result in a parse error, such as:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// This of course applies to using superglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";
// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple
有关详细信息,请参阅。-1$row['id']无法在双引号内正确展开。这不会修复任何问题。这将导致相同的分析错误。我一直使用它,它可以正常工作correctly@MBAsfoor:不,您不需要。您可能会编写
$newLI=“”;
如果有的话。你说得对,我很抱歉。对象属性需要大括号,但数组键不需要大括号……当我在写我的答案时出现5个答案时,我既喜欢也讨厌stackoverflow:(
$rowID = $row['id'];
$newLI = "<li id=$rowID style=\"padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;\">"
<?php
$newLI = "<li id =".$row['id']." style=\"padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;\">";
?>
// The following is okay, as it's inside a string. Constants are not looked for
// within strings, so no E_NOTICE occurs here
print "Hello $arr[fruit]"; // Hello apple
// With one exception: braces surrounding arrays within strings allows constants
/ / to be interpreted
print "Hello {$arr[fruit]}"; // Hello carrot
print "Hello {$arr['fruit']}"; // Hello apple
// This will not work, and will result in a parse error, such as:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// This of course applies to using superglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";
// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple