admin管理员组文章数量:1318573
I need your help. I have this code that I need to change
$metMessage = "";
foreach($batches as $batch)
{
$metMessage .= '<tr>
<td>' . $batch['comp_code'] . '</td>
<td>' . $batch['description'] . '</td>
<td>' . number_format($batch['quantity'], 0, '.', '') .'</td>
</tr>';
}
What i need:
If $batch['quantity'] < 3 then .
number_format($batch['quantity'], 2, '.', '') .'
else .
number_format($batch['quantity'], 0, '.', '') .'
I don't know how to add this condition
I need your help. I have this code that I need to change
$metMessage = "";
foreach($batches as $batch)
{
$metMessage .= '<tr>
<td>' . $batch['comp_code'] . '</td>
<td>' . $batch['description'] . '</td>
<td>' . number_format($batch['quantity'], 0, '.', '') .'</td>
</tr>';
}
What i need:
If $batch['quantity'] < 3 then .
number_format($batch['quantity'], 2, '.', '') .'
else .
number_format($batch['quantity'], 0, '.', '') .'
I don't know how to add this condition
Share Improve this question edited Jan 21 at 11:29 ADyson 62.1k16 gold badges78 silver badges91 bronze badges Recognized by PHP Collective asked Jan 21 at 10:52 user21024361user21024361 32 bronze badges 4 |1 Answer
Reset to default 2One way to do this neatly within the concatentation you've already got is using a ternary operator rather than an if/else
block.
For example:
number_format($batch['quantity'], ($batch['quantity'] < 3 ? 2 : 0), '.', '')
本文标签: phpI need to change a field numberformatStack Overflow
版权声明:本文标题:php - I need to change a field number_format - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742048383a2417927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
if
/else
in there directly. You could use the ternary operator, or split this into multiple parts (so that you can use an if/else in between.) Or you could use a variable for the second parameter ofnumber_format
, and then simply set that variable to the appropriate 2 or 0, before the whole$metMessage .= ...
– C3roe Commented Jan 21 at 11:09