php switch case的"bug"

本文解析了PHP中switch语句的一个常见陷阱,当条件表达式的值为0时导致的非预期行为,并提供了一种修正方法。

首先说明,这不是一个bug。应该说是一个比较容易中招的陷阱。


今天使用switch遇到一个问题,代码如下:

 1 <?php
 2 
 3 
 4 $num = 0;
 5 switch ($price) {
 6     case $price <= 100:
 7         $price_between = "100以下";
 8         break;
 9
<?php // 禁止缓存 header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); header('Expires: 0'); // 开发环境清除 OPCache 缓存 if (function_exists('opcache_invalidate')) { opcache_invalidate(__FILE__, true); } // 数据库配置 $host = '127.0.0.1'; $dbname = 'db_hk4e_config_gio'; $username = 'root'; $password = 'qq28009618'; // 请根据实际情况修改密码 $dsn = &quot;mysql:host=$host;dbname=$dbname;charset=utf8mb4&quot;; try { $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_PERSISTENT, false); } catch (PDOException $e) { die(&quot;数据库连接失败: &quot; . htmlspecialchars($e->getMessage())); } // 处理表单提交 if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save'])) { $allowed_fields = [ 'begin_time', 'end_time', 'gacha_up_config', 'gacha_prefab_path', 'gacha_preview_prefab_path', 'title_textmap' ]; foreach ($_POST['rows'] as $key => $data) { $schedule_id = trim(urldecode($key)); $setParts = []; $params = []; foreach ($allowed_fields as $field) { if (isset($data[$field])) { $value = trim($data[$field]); $setParts[] = &quot;`$field` = ?&quot;; $params[] = $value; } } if (!empty($setParts)) { $params[] = $schedule_id; $sql = &quot;UPDATE `t_gacha_schedule_config` SET &quot; . implode(', ', $setParts) . &quot; WHERE `schedule_id` = ?&quot;; try { $stmt = $pdo->prepare($sql); $stmt->execute($params); } catch (Exception $ex) { echo &quot;<div style='color:red;'>更新失败 (ID: $schedule_id): &quot; . htmlspecialchars($ex->getMessage()) . &quot;</div><br>&quot;; } } } // 显示成功提示并自动刷新 echo &quot;<div style='color:green; font-weight:bold;'>✅ 所有更改已成功保存!</div><br>&quot;; // 重新查询最新数据 $stmt = $pdo->query(&quot;SELECT schedule_id, begin_time, end_time, gacha_up_config, gacha_prefab_path, gacha_preview_prefab_path, title_textmap FROM t_gacha_schedule_config&quot;); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); } else { try { $stmt = $pdo->query(&quot;SELECT schedule_id, begin_time, end_time, gacha_up_config, gacha_prefab_path, gacha_preview_prefab_path, title_textmap FROM t_gacha_schedule_config&quot;); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (Exception $e) { die(&quot;<div style='color:red;'>查询失败: &quot; . htmlspecialchars($e->getMessage()) . &quot;</div>&quot;); } } // 根据 schedule_id 返回卡池名称 function getPoolName($id) { switch ((int)$id) { case 893: return '角色UP1卡池'; case 903: return '角色UP2卡池'; case 913: return '角色常驻卡池'; case 923: return '武器UP1卡池'; case 933: return '武器常驻卡池'; default: return &quot;未知卡池($id)&quot;; } } ?> <!DOCTYPE html> <html lang=&quot;zh&quot;> <head> <meta charset=&quot;UTF-8&quot;> <title>抽卡活动配置 - 五星/四星UP编辑</title> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 20px; background-color: #f9f9f9; color: #333; } h1 { color: #2c3e50; } table { width: 100%; border-collapse: collapse; margin-top: 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); background-color: white; } th, td { border: 1px solid #ddd; padding: 10px; text-align: left; vertical-align: top; } th { background-color: #4CAF50; color: white; } tr:nth-child(even) { background-color: #f2f2f2; } input[type=&quot;text&quot;], input[type=&quot;datetime-local&quot;], textarea { width: 100%; padding: 5px; box-sizing: border-box; font-family: monospace; border: 1px solid #ccc; border-radius: 4px; } .action { text-align: center; margin-bottom: 20px; } button { padding: 10px 20px; font-size: 16px; background-color: #007BFF; color: white; border: none; border-radius: 6px; cursor: pointer; transition: background-color 0.3s; } button:hover { background-color: #0056b3; } .pool-label { display: inline-block; padding: 5px 10px; font-size: 14px; background-color: #607D8B; color: white; border: none; border-radius: 4px; margin-right: 10px; text-align: center; min-width: 100px; height: 32px; line-height: 16px; vertical-align: middle; } .star-btn { margin-right: 10px; background-color: #e91e63; color: white; padding: 5px 10px; font-size: 14px; border: none; border-radius: 4px; cursor: pointer; height: 32px; line-height: 16px; vertical-align: middle; } .star-btn:hover { background-color: #c2185b; } .modal { display: none; position: fixed; z-index: 2000; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.5); align-items: center; justify-content: center; } .modal-content { background-color: #fff; padding: 25px; border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,0.3); width: 400px; max-width: 90%; text-align: center; animation: fadeIn 0.3s ease-out; } @keyframes fadeIn { from { opacity: 0; transform: scale(0.9); } to { opacity: 1; transform: scale(1); } } .modal-header { font-size: 1.4em; margin-bottom: 15px; color: #333; } .modal-body { margin: 15px 0; color: #555; font-size: 1.1em; } .modal-footer { margin-top: 20px; display: flex; gap: 10px; justify-content: center; } .btn-confirm { background-color: #28a745; } .btn-cancel { background-color: #dc3545; } .btn-confirm:hover { background-color: #218838; } .btn-cancel:hover { background-color: #c82333; } .close { float: right; font-size: 24px; font-weight: bold; cursor: pointer; color: #aaa; } .close:hover { color: #000; } /* 固定宽度复选框容器 */ .item-list { max-height: 300px; overflow-y: auto; border: 1px solid #ddd; padding: 10px; margin-top: 10px; border-radius: 4px; text-align: left; } .item-list label { display: flex; align-items: center; margin: 5px 0; padding: 4px 0; cursor: pointer; font-size: 14px; } .item-list input[type=&quot;checkbox&quot;] { margin-right: 10px; min-width: 18px; height: 18px; } .item-list span { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } </style> </head> <body> <h1>抽卡活动配置表 - 支持五星/四星UP编辑 💯</h1> <p><strong>📌 提示:每行左侧显示卡池类型,点击 ⭐ 或 ⭐⭐ 按钮选择UP角色</strong></p> <form method=&quot;post&quot; id=&quot;editForm&quot;> <?php if ($results): ?> <table> <thead> <tr> <th>开始时间</th> <th>结束时间</th> <th>UP 配置</th> <th>Prefab 路径</th> <th>预览 Prefab 路径</th> <th>标题文本</th> </tr> </thead> <tbody> <?php foreach ($results as $row): ?> <tr> <!-- 开始时间 --> <td> <input type=&quot;datetime-local&quot; name=&quot;rows[<?= urlencode($row['schedule_id']) ?>][begin_time]&quot; value=&quot;<?= date('Y-m-d\TH:i', strtotime($row['begin_time'])) ?>&quot; step=&quot;60&quot; autocomplete=&quot;off&quot;> </td> <!-- 结束时间 --> <td> <input type=&quot;datetime-local&quot; name=&quot;rows[<?= urlencode($row['schedule_id']) ?>][end_time]&quot; value=&quot;<?= date('Y-m-d\TH:i', strtotime($row['end_time'])) ?>&quot; step=&quot;60&quot; autocomplete=&quot;off&quot;> </td> <!-- UP 配置 --> <td> <textarea name=&quot;rows[<?= urlencode($row['schedule_id']) ?>][gacha_up_config]&quot; rows=&quot;3&quot; style=&quot;width:100%; resize:none;&quot; placeholder='{&quot;gacha_up_list&quot;:[{&quot;item_parent_type&quot;:1,...}]}' autocomplete=&quot;off&quot;><?= htmlspecialchars($row['gacha_up_config']) ?></textarea> <!-- 卡池名称 + 按钮组 --> <div style=&quot;margin-top: 5px;&quot;> <!-- 卡池名称(只读) --> <span class=&quot;pool-label&quot;> <?= htmlspecialchars(getPoolName($row['schedule_id'])) ?> </span> <!-- 五星按钮 --> <button type=&quot;button&quot; class=&quot;star-btn&quot; data-target=&quot;rows[<?= urlencode($row['schedule_id']) ?>][gacha_up_config]&quot; data-star=&quot;5&quot;> ⭐ 五星 </button> <!-- 四星按钮 --> <button type=&quot;button&quot; class=&quot;star-btn&quot; data-target=&quot;rows[<?= urlencode($row['schedule_id']) ?>][gacha_up_config]&quot; data-star=&quot;4&quot;> ⭐⭐ 四星 </button> </div> </td> <!-- Prefab 路径 --> <td> <input type=&quot;text&quot; name=&quot;rows[<?= urlencode($row['schedule_id']) ?>][gacha_prefab_path]&quot; value=&quot;<?= htmlspecialchars($row['gacha_prefab_path']) ?>&quot; autocomplete=&quot;off&quot;> </td> <!-- 预览 Prefab 路径 --> <td> <input type=&quot;text&quot; name=&quot;rows[<?= urlencode($row['schedule_id']) ?>][gacha_preview_prefab_path]&quot; value=&quot;<?= htmlspecialchars($row['gacha_preview_prefab_path']) ?>&quot; autocomplete=&quot;off&quot;> </td> <!-- 标题文本 --> <td> <input type=&quot;text&quot; name=&quot;rows[<?= urlencode($row['schedule_id']) ?>][title_textmap]&quot; value=&quot;<?= htmlspecialchars($row['title_textmap']) ?>&quot; autocomplete=&quot;off&quot;> </td> </tr> <?php endforeach; ?> </tbody> </table> <div class=&quot;action&quot;> <button type=&quot;button&quot; id=&quot;showConfirmModal&quot;>💾 保存所有修改</button> </div> <?php else: ?> <p>❌ 没有找到任何数据,请检查数据库。</p> <?php endif; ?> </form> <!-- 自定义确认模态框 --> <div id=&quot;confirmModal&quot; class=&quot;modal&quot;> <div class=&quot;modal-content&quot;> <div class=&quot;modal-header&quot;>⚠️ 确认保存</div> <div class=&quot;modal-body&quot;> 确定要保存所有修改吗?<br> 此操作将提交更改并刷新页面。 </div> <div class=&quot;modal-footer&quot;> <button class=&quot;btn-confirm&quot; id=&quot;confirmSave&quot;>✅ 确定</button> <button class=&quot;btn-cancel&quot; id=&quot;cancelSave&quot;>❌ 取消</button> </div> </div> </div> <!-- 角色选择模态框 --> <div id=&quot;upModal&quot; class=&quot;modal&quot;> <div class=&quot;modal-content&quot;> <span class=&quot;close&quot;>×</span> <h3 id=&quot;modalTitle&quot;>选择UP角色</h3> <div id=&quot;itemList&quot; class=&quot;item-list&quot;></div> <p style=&quot;text-align:center;&quot;><button onclick=&quot;applySelection()&quot;>🟢 确认选择</button></p> </div> </div> <script> let currentTextarea = null; let currentStarLevel = 5; // 绑定按钮事件 document.querySelectorAll('.star-btn').forEach(btn => { btn.addEventListener('click', function () { currentTextarea = document.querySelector(`[name=&quot;${this.dataset.target}&quot;]`); currentStarLevel = parseInt(this.dataset.star); document.getElementById('modalTitle').textContent = currentStarLevel === 5 ? '选择五星UP角色' : '选择四星UP角色'; document.getElementById('upModal').style.display = 'flex'; loadItems(currentStarLevel === 5 ? 'kachi5.txt' : 'kachi4.txt'); }); }); // 关闭模态框 document.querySelector('#upModal .close').onclick = function () { document.getElementById('upModal').style.display = 'none'; }; window.onclick = function (event) { const upModal = document.getElementById('upModal'); const confirmModal = document.getElementById('confirmModal'); if (event.target === upModal) { upModal.style.display = 'none'; } if (event.target === confirmModal) { confirmModal.style.display = 'none'; } }; // 加载 txt 文件 function loadItems(filename) { fetch(filename + '?t=' + Date.now()) .then(res => { if (!res.ok) throw new Error('文件未找到: ' + filename); return res.text(); }) .then(text => { const lines = text.split('\n').filter(line => line.trim()); const container = document.getElementById('itemList'); container.innerHTML = ''; lines.forEach(line => { const match = line.trim().match(/^(\d+):(.+)$/); if (match) { const id = match[1]; const name = match[2].trim(); const label = document.createElement('label'); label.style.display = 'flex'; label.style.alignItems = 'center'; label.style.margin = '5px 0'; const checkbox = document.createElement('input'); checkbox.type = 'checkbox'; checkbox.value = id; checkbox.style.minWidth = '18px'; checkbox.style.height = '18px'; checkbox.style.marginRight = '10px'; const span = document.createElement('span'); span.textContent = `${id}: ${name}`; span.style.whiteSpace = 'nowrap'; span.style.overflow = 'hidden'; span.style.textOverflow = 'ellipsis'; label.appendChild(checkbox); label.appendChild(span); container.appendChild(label); } }); }) .catch(err => { alert('加载失败: ' + err.message); }); } // 应用选择结果 ✅ 已修复 bug function applySelection() { if (!currentTextarea) { alert(&quot;内部错误:未绑定目标文本域!&quot;); return; } const selectedIds = Array.from(document.querySelectorAll('#itemList input:checked')) .map(el => parseInt(el.value)) .filter(id => !isNaN(id)); if (selectedIds.length === 0) { alert(&quot;请至少选择一个角色!&quot;); return; } let rawData, upListArray, wasWrapped = false; try { const rawText = currentTextarea.value.trim(); if (!rawText) { rawData = { gacha_up_list: [] }; wasWrapped = true; } else { rawData = JSON.parse(rawText); } if (rawData && typeof rawData === 'object' && Array.isArray(rawData.gacha_up_list)) { upListArray = rawData.gacha_up_list; wasWrapped = true; } else if (Array.isArray(rawData)) { upListArray = rawData; wasWrapped = false; } else { throw new Error(&quot;根节点必须是数组或包含 gacha_up_list 的对象&quot;); } } catch (e) { alert(&quot;UP配置字段不是合法JSON,请检查。\n错误:&quot; + e.message); return; } const targetType = currentStarLevel === 5 ? 1 : 2; // 1=五星, 2=四星 // ✅ 修复了这里:item.item_list 不是 item_list const existingIndex = upListArray.findIndex(item => item.item_parent_type === targetType && item.item_list && Array.isArray(item.item_list) // ✅ 正确写法 ); if (existingIndex > -1) { upListArray[existingIndex].item_list = selectedIds; } else { upListArray.push({ item_parent_type: targetType, prob: targetType === 1 ? 500 : 600, item_list: selectedIds }); } const output = wasWrapped ? JSON.stringify({ gacha_up_list: upListArray }, null, 0) : JSON.stringify(upListArray, null, 0); try { currentTextarea.value = output; alert(`✅ 已更新 ${selectedIds.length} 个角色为UP项!`); document.getElementById('upModal').style.display = 'none'; } catch (e) { alert(&quot;序列化失败:&quot; + e.message); } } // 显示确认模态框 document.getElementById('showConfirmModal').addEventListener('click', function () { document.getElementById('confirmModal').style.display = 'flex'; }); // 取消保存 document.getElementById('cancelSave').addEventListener('click', function () { document.getElementById('confirmModal').style.display = 'none'; }); // 确定保存 → 提交表单 document.getElementById('confirmSave').addEventListener('click', function () { document.getElementById('confirmModal').style.display = 'none'; const form = document.getElementById('editForm'); const input = document.createElement('input'); input.type = 'hidden'; input.name = 'save'; input.value = '1'; form.appendChild(input); form.submit(); // 提交后页面会刷新 }); </script> </body> </html> 不更改内容 只删除 注释
最新发布
11-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kingofyz

此处弱弱求打赏~~万一有好心人

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值