我正在试图弄清楚这个错误的来源(我正在尝试使用PHP输出一些HTML)。
$newLI = "<li id = $row['\"id\"'] style=\"padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;\">";
最终给了我:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /homepages/31/d346239161/htdocs/Bloominate/alpha/getProfileData.php on line 23
我无法弄清楚解析错误的位置:(。
答案 0 :(得分:5)
你需要用花括号括起$ 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;'>";
答案 1 :(得分:5)
使用单引号和字符串连接可能更容易避免这些错误:
$newLI = '<li id="'.$row['id'].'" style="padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;">';
此外,您不必转义HTML代码的双引号。
答案 2 :(得分:2)
你的问题是$ 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
答案 3 :(得分:1)
实际答案是: -
$newLI = '<li id = '.$row['id'].' style="padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;">';
希望它有所帮助。
答案 4 :(得分:0)
更改
$newLI = "<li id = $row['\"id\"'] style=\"padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;\">"
要:
$rowID = $row['id'];
$newLI = "<li id=$rowID style=\"padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;\">"
这是因为你引用了$ row ['id']。
答案 5 :(得分:0)
为什么不保持简洁如下:
<?php
$newLI = "<li id =".$row['id']." style=\"padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;\">";
?>
答案 6 :(得分:-2)
试试这个
$newLI = "<li id = $row['id'] style='padding-right: 20px; display:inline; border-right: 1px gray solid; margin: 0px; color: white;'>";