【发布时间】:2014-12-15 11:23:21
【问题描述】:
这是我第一次学习三元运算符。我在这里要做的是将一些 php 语句行转换为三元运算符。谁能帮我检查我在这里所做的是否正确。以及如何回应它。谢谢。
<?php
$tmp = 'this.ppt';
$tail = array_pop(explode('.',$tmp)); //'{file}'
$allow = array('ppt','pdf','docx');
if (in_array($tail, $allow) {
$type = $tail;
}
elseif ($tail == 'doc') {
$type = 'docx';
}
else {
$type = 'img';
}
echo $type;
?>
TP
$tail = ($type == $tail ? 'ppt','pdf','docx' : ($type == 'doc') ? 'docx' : 'img()'))
【问题讨论】:
-
您的原始代码:1. 可读 2. 有效 3. 易于维护。你的新尝试:3个都不是。
标签: php ternary-operator