【问题标题】:How to use Php Comparison and Ternary Operators in this code?如何在这段代码中使用 PHP 比较和三元运算符?
【发布时间】:2018-08-03 21:32:03
【问题描述】:

我一直在阅读documents 并试图让一段代码工作无济于事。如果值与数据库中的值匹配,我想检查无线电输入。我正在发布相关的代码。

原if语句

if(isset($_POST['radioValue'])){
if($prospectAnswer == $script[$getScriptAA]){
echo 'checked="checked';}}

到目前为止我已经尝试过什么......

$input .=  ' <input  
'.((isset($_POST['radioValue']) 
&& ($prospectAnswer == $script[$getScriptAA])) ? 'checked="checked').'
              type="radio" 
              name="sAnswer" 
              value="'.$script[$getScriptAA].'">
<span class="radiotext"> '.$script[$getScriptAA].'</span>';

【问题讨论】:

  • 您缺少“:”。如(condition) ? checked : unchecked.

标签: php concatenation ternary-operator


【解决方案1】:

您的代码似乎缺少三元组的第二部分,请在'checked="checked"' 之后添加: ''

$input .=  ' <input  
'.(isset($_POST['radioValue']) && $prospectAnswer == $script[$getScriptAA] ? 'checked="checked"' : '').'
              type="radio" 
              name="sAnswer" 
              value="'.$script[$getScriptAA].'">
<span class="radiotext"> '.$script[$getScriptAA].'</span>';

【讨论】:

    猜你喜欢
    • 2017-01-01
    • 2020-06-05
    • 2016-06-17
    • 2018-06-27
    • 2015-06-10
    • 2012-10-05
    • 2014-12-19
    • 1970-01-01
    • 2011-07-08
    相关资源
    最近更新 更多