2014-05-20 72 views
0

因此,我只是通過PHP語句構建了一個表,但我不確定如何添加border="1"屬性,因爲這會破壞echo語句並導致編譯錯誤。在echo語句中向表中添加「border」

這裏是我的代碼,是的,它看起來很嚇人這種格式,但我只需要給表格一個border標籤莫名其妙。

echo 
"<table><tr> 
<th></th> 
<th>A</th> 
<th>B</th> 
<th>AB</th> 
<th>O</th> 

</tr><tr> 

<th>N</th> 
<th>" . $ATypeN . "</th> 
<th>" . $BTypeN . "</th> 
<th>" . $ABTypeN . "</th> 
<th>" . $OTypeN . "</th> 
<th>". 

"</tr><tr> 

<th>Y</th> 
<th>" . $ATypeY . "</th> 
<th>" . $BTypeY . "</th> 
<th>" . $ABTypeY . "</th> 
<th>" . $OTypeY . "</th> 
</tr> 
</table>"; 

回答

5

你需要立即引號字符前escape\(反斜槓)的報價,如:

echo "<table border=\"0\"><tr>"; 

您還可以使用雙引號內的單引號,反之亦然,如:

echo '<table border="0"><tr>'; 

或:

echo "<table border='0'><tr>"; 

評論者指出HEREDOC方法,這對您也有很大的價值。以相同的標識符開始和結束:

/* start with "EOT", must also terminate with "EOT" followed by a semicolon */ 

echo <<<EOT 
<table><tr> 
<th></th> 
<th>A</th> 
<th>B</th> 
<th>AB</th> 
<th>O</th> 

</tr><tr> 

<th>N</th> 
<th>$ATypeN</th> 
<th>$BTypeN</th> 
<th>$ABTypeN</th> 
<th>$OTypeN</th> 
</tr><tr> 

<th>Y</th> 
<th>$ATypeY</th> 
<th>$BTypeY</th> 
<th>$ABTypeY</th> 
<th>$OTypeY</th> 
</tr> 
</table> 
EOT; /* terminated here, cannot be indented, line must contain only EOT; */ 
+1

我也將顯示定界符,因爲這將是最好的OP的當前代碼。 – Jessica

+1

另外值得指出的是,你不需要連接像'「。$ variable這樣的變量,」「在雙引號內。你可以直接使用它們,如:' $ variable' –

+0

@SetSailMedia謝謝!我覺得很愚蠢,我甚至沒有考慮過PHP中的轉義字符。 :)當計時器啓動時,我會將其標記爲答案! – Austin

1

儘管上面的答案指出了您的錯誤,應該指出一些事情。

如果您正在使用單引號,你不需要逃避你的雙引號:

所以

echo '<table border=\"0\"><tr>'; 

應該

echo '<table border="0"><tr>'; 

而且使用單引號和逗號來連接蜇傷執行時間比使用雙引號和句點更快;雙引號中的所有內容都會被評估。

所以你也可以做

echo '<table><tr><td>',$someValue,'</td></tr></table>'; 

另一種方式,你也可以做到這一點是寫出來的HTML,然後回聲出像下面的變量,你將有語法的利益凸顯你的HTML在你的文本編輯器中。

<table> 
    <tr> 
     <td><?php echo $someValue ?></td> 
    </tr> 
</table> 

或啓用PHP短標籤(這我不是一個風扇)

<table> 
    <tr> 
     <td><?=$someValue ?></td> 
    </tr> 
</table> 
+0

感謝您的額外信息! – Austin