You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP数组内添加if条件判断为:status字段动态赋值的实现方法

解决方案

你可以直接使用PHP三元运算符实现条件判断,:status 对应位置的代码如下:

($_POST["name"] == 1 && $_POST["dob"] == 1 && $_POST["barcode"] == 1) ? 100 : 0

补充说明

  • 以上写法为弱类型判断,若要求值和类型都完全匹配为数字1,将所有==替换为===即可
  • 如果需要规避三个字段不存在导致的PHP未定义索引报错,可以加上isset判断:
    (isset($_POST["name"], $_POST["dob"], $_POST["barcode"]) && $_POST["name"] == 1 && $_POST["dob"] == 1 && $_POST["barcode"] == 1) ? 100 : 0
    
  • 完整的数组拼接示例:
if ($_POST["action"] == 'Add') {
    $data = array(
        ':visitor_enter_time'   =>  $visitor->get_datetime(),
        ':visitor_name'         => $_POST["visitor_name"],
        ':no_rm'        =>  $_POST["noRm"],
        ':options'  =>  $_POST["options"],
        ':name' =>  $_POST["name"],
        ':dob' =>   $_POST["dob"],
        ':barcode' =>   $_POST["barcode"],
        ':status' => ($_POST["name"] == 1 && $_POST["dob"] == 1 && $_POST["barcode"] == 1) ? 100 : 0,
        ':id_pic'       =>  $_SESSION["admin_id"]
    );

内容的提问来源于stack exchange,提问作者Gus Dirga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 07:09:02