Php 使代码更小的可能性
有没有办法使我的代码更小?我的意思是,我打赌有一种方法可以使它变小,但我不知道,我有下面的代码充满了Php 使代码更小的可能性,php,Php,有没有办法使我的代码更小?我的意思是,我打赌有一种方法可以使它变小,但我不知道,我有下面的代码充满了if: if($CType != NULL) { echo '<th align="right">CType </th>'; }else{ } if($CNr != NULL)
if
:
if($CType != NULL)
{
echo '<th align="right">CType </th>';
}else{
}
if($CNr != NULL)
{
echo '<th align="right">CNr </th>';
}else{
}
if($CValFrom != NULL)
{
echo '<th align="right">CValFrom </th>';
}else{
}
if($CValUntil != NULL)
{
echo '<th align="right">CValUntil </th>';
}else{
}
if($CLTCCNr != NULL)
{
echo '<th align="right">CLTCCNr </th>';
}else{
}
if($CLTCCTime != NULL)
{
echo '<th align="right">CLTCCTime </th>';
}else{
}
if($CIOEntryOff != NULL)
{
echo '<th align="right">CIOEntryOff </th>';
}else{
}
if($CIOExitOff != NULL)
{
echo '<th align="right">CIOExitOff </th>';
}else{
}
if($CICoded != NULL)
{
echo '<th align="right">CICoded </th>';
}else{
}
if($CCodingDate != NULL)
{
echo '<th align="right">CCodingDate </th>';
}else{
}
if($CPrice != NULL)
{
echo '<th align="right">CPrice </th>';
}else{
}
if($CPricePyed != NULL)
{
echo '<th align="right">CPricePyed </th>';
}else{
}
if($CDeposit != NULL)
{
echo '<th align="right">CDeposit </th>';
}else{
}
if($CDepositPayed != NULL)
{
echo '<th align="right">CDepositPayed </th>';
}else{
}
if($CLastCCode != NULL)
{
echo '<th align="right">CLastCCode </th>';
}else{
}
if($CLastLPlate != NULL)
{
echo '<th align="right">CLastLPlate </th>';
}else{
}
if($CIOOffNextUse != NULL)
{
echo '<th align="right">CIOOffNextUse </th>';
}else{
}
if($CLastEntryTime != NULL)
{
echo '<th align="right">CLastEntryTime </th>';
}else{
}
if($CLastExitTime != NULL)
{
echo '<th align="right">CLastExitTime </th>';
}else{
}
if($CLastPTime != NULL)
{
echo '<th align="right">CLastPTime </th>';
}else{
}
if($CType!=NULL)
{
回声“CType”;
}否则{
}
如果($CNr!=NULL)
{
回声“CNr”;
}否则{
}
如果($CValFrom!=NULL)
{
回声“CValFrom”;
}否则{
}
如果($CValUntil!=NULL)
{
echo‘CValUntil’;
}否则{
}
如果($CLTCCNr!=NULL)
{
回声“CLTCCNr”;
}否则{
}
如果($cltctime!=NULL)
{
回声“cltctime”;
}否则{
}
如果($cientryoff!=NULL)
{
回声“Cientryoff”;
}否则{
}
如果($CIOExitOff!=NULL)
{
回声“CIOExitOff”;
}否则{
}
如果($CICoded!=NULL)
{
回声“循环码”;
}否则{
}
如果($CCodingDate!=NULL)
{
回音“CCodingDate”;
}否则{
}
如果($CPrice!=NULL)
{
回声“CPrice”;
}否则{
}
如果($CPricePyed!=NULL)
{
回声“CPricePyed”;
}否则{
}
如果($CDeposit!=NULL)
{
回声“CDeposit”;
}否则{
}
如果($CDepositPayed!=NULL)
{
回声“CDepositPayed”;
}否则{
}
如果($clastcode!=NULL)
{
回声“碎屑代码”;
}否则{
}
如果($clastalplate!=NULL)
{
回声“碎屑板”;
}否则{
}
如果($CIOOffNextUse!=NULL)
{
回声“CIOOffNextUse”;
}否则{
}
如果($clasentrytime!=NULL)
{
回声“进入时间”;
}否则{
}
如果($CLastExitTime!=NULL)
{
回声“碎屑时间”;
}否则{
}
如果($CLastPTime!=NULL)
{
回音“碎裂时间”;
}否则{
}
我怎样才能把它缩短有什么办法吗?它应该以同样的方式工作。有很多方法可以做到这一点 其中一项:
$tableHeaders = [
['variable' => 'CType', 'title' => 'Tipo'],
['variable' => 'CNr', 'title' => 'Nr.'],
...
];
foreach($tableHeaders as $tableHeader) :
$variable = $tableHeader['variable'];
if (!isset($$variable) OR is_null($$variable)) continue;
?>
<th align="right"><?= $tableHeader['title']; ?></th>
<?php
endforeach;
?>
$tableHeaders=[
['variable'=>'CType','title'=>'Tipo'],
['variable'=>'CNr','title'=>'Nr.],
...
];
foreach($tableHeaders作为$tableHeader):
$variable=$tableHeader['variable'];
如果(!isset($$variable)或为空($$variable))继续;
?>
但推荐的方法是使用普通模板引擎,让它来渲染数据集本身这真的应该发布到。哦,我不知道,我会在那里发布,谢谢…给我们一个链接:)只是为了挑战谁可以把它变小如果你只想把它变小,然后删除你写的所有其他内容,因为你在其他方面什么都没做!u l保存18行:D