在PHP中如何在td标记内使用简单的if语句

时间:2018-03-30 11:49:56

标签: javascript php html

我的PHP脚本具有以下if语句:

if($idx = strripos($output,','))//Get the last index of ',' in your output string
{
 $ErrorCode = substr($output,$idx + 1,(strlen($output) - $idx) - 1); 
 //$Playlist = substr($output, 0, $idx + 1); //Get the rest of the output string, minus the error code 
 echo " " .$Playlist.ReturnError($ErrorCode); //The ReturnError function just replaces the error code with a custom error

如何在最后一个td标记内使用if语句:

//php script continued..

echo '<table style="width:50%">
<tr>
<th>Status</th>
<th>Name</th>
<th>Date/Time</th>
<th>Playing</th>
<th>Error</th>
</tr>
<tr>
<td></td>
<td>Test</td>
<td>'.$array[0].'</td>
<td>'.$array[1].'</td>
<td></td>
</tr>

</table>';

我已经查看了其他帖子的各种示例,但我无法弄清楚如何使其正常工作。

3 个答案:

答案 0 :(得分:0)

//php script continued..

echo '<table style="width:50%">
<tr>
<th>Status</th>
<th>Name</th>
<th>Date/Time</th>
<th>Playing</th>
<th>Error</th>
</tr>
<tr>
<td></td>
<td>Test</td>
<td>'.$array[0].'</td>
<td>'.$array[1].'</td>
<td>';
if($idx = strripos($output,','))//Get the last index of ',' in your output string
{
 $ErrorCode = substr($output,$idx + 1,(strlen($output) - $idx) - 1); 
 //$Playlist = substr($output, 0, $idx + 1); //Get the rest of the output string, minus the error code 
 echo " " .$Playlist.ReturnError($ErrorCode); //The ReturnError function just replaces the error code with a custom error
}

echo'</td>
</tr>

</table>';

答案 1 :(得分:0)

您有2个选项

三元运营商http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

echo('<h1>' . isset($array[0]) ? $array[0] : 'default' . '</h1>');

Null Coalescing Operator(仅限php 7)http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce

echo('<h1>' . $array[0] ?? 'default' . '</h1>');

defaullt的位置,您可以执行所有操作,例如调用方法,显示错误消息等。

答案 2 :(得分:0)

Use this

//php script continued..

$str='<table style="width:50%">
<tr>
<th>Status</th>
<th>Name</th>
<th>Date/Time</th>
<th>Playing</th>
<th>Error</th>
</tr>
<tr>
<td></td>
<td>Test</td>
<td>'.$array[0].'</td>
<td>'.$array[1].'</td>
<td>';
if($idx = strripos($output,','))//Get the last index of ',' in your output string
{
 $ErrorCode = substr($output,$idx + 1,(strlen($output) - $idx) - 1); 
 //$Playlist = substr($output, 0, $idx + 1); //Get the rest of the output string, minus the error code 
 $str.= " " .$Playlist.ReturnError($ErrorCode); //The ReturnError function just replaces the error code with a custom error

}

$str.='</td>
</tr>

</table>';