PHP 中 JSON 文件写入错误的排查与修复指南

1次阅读

PHP 中 JSON 文件写入错误的排查与修复指南

本文详解 php 操作 jsON 文件时因误将函数名 json_encode 写成变量 $json_encode 导致的致命错误,提供完整修复方案、安全编码实践及常见陷阱总结。

本文详解 php 操作 json 文件时因误将函数名 `json_encode` 写成变量 `$json_encode` 导致的致命错误,提供完整修复方案、安全编码实践及常见陷阱总结。

在 PHP 开发中,动态读写 JSON 配置或数据文件(如待办清单 todo.json)是常见需求。但初学者常因语法混淆引发严重运行时错误——正如示例中所示:

file_put_contents('todo.json', $json_encode($jsonArray, JSON_PRETTY_PRINT));

该行代码试图调用一个名为 $json_encode 的变量(带 $ 前缀),而实际应调用的是内置函数 json_encode()。PHP 解析器因此报出两个关键错误:

  • Warning: undefined variable $json_encode(未定义变量警告)
  • Fatal Error: Value of type NULL is not callable(空值不可调用,因未定义变量默认为 null)

正确写法(修正后):

file_put_contents('todo.json', json_encode($jsonArray, JSON_PRETTY_PRINT));

⚠️ 但仅修复拼写仍不够!以下是生产级 JSON 文件操作的必备实践:

立即学习PHP免费学习笔记(深入)”;

1. 增加错误处理,避免静默失败

$json = file_get_contents('todo.json'); if ($json === false) {     die("Error: Failed to read todo.json"); }  $jsonArray = json_decode($json, true); if (json_last_error() !== JSON_ERROR_NONE) {     die("Error: Invalid JSON format in todo.json"); }

2. 写入前验证并确保目录可写

$filePath = 'todo.json'; if (!is_writable(dirname($filePath))) {     die("Error: Directory containing {$filePath} is not writable."); }

3. 使用原子写入,防止数据损坏(推荐)

$tempFile = $filePath . '.tmp'; $result = file_put_contents($tempFile, json_encode($jsonArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); if ($result === false) {     unlink($tempFile);     die("Error: Failed to write temporary file."); } if (!rename($tempFile, $filePath)) {     unlink($tempFile);     die("Error: Failed to replace original file atomically."); }

4. 完整健壮示例(含表单提交处理)

<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') {     $todoname = $_POST['todoname'] ?? '';     $todoname = trim($todoname);      if (empty($todoname)) {         die("Error: Todo name cannot be empty.");     }      $filePath = 'todo.json';      // 读取并解析     $json = file_get_contents($filePath);     if ($json === false) throw new Exception("Cannot read {$filePath}");     $data = json_decode($json, true);     if (json_last_error() !== JSON_ERROR_NONE) throw new Exception("Invalid JSON in {$filePath}");      // 添加新条目(使用时间戳作唯一键更安全,避免重复名称冲突)     $data[] = [         'id' => uniqid(),         'name' => $todoname,         'completed' => false,         'created_at' => date('c')     ];      // 原子写入     $temp = $filePath . '.tmp';     if (file_put_contents($temp, json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)) === false) {         unlink($temp);         throw new Exception("Write failed");     }     rename($temp, $filePath);      echo "✓ Todo added successfully."; } ?>

? 关键总结:

  • ✅ 函数名无 $,变量名必须有 $ —— 这是 PHP 基础语法铁律;
  • ✅ 永远校验 file_get_contents 和 json_decode 的返回值;
  • ✅ 使用 JSON_UNESCAPED_UNICODE 避免中文被转义;
  • ✅ 优先采用原子写入(临时文件 + rename),杜绝并发写入导致的数据截断;
  • ✅ 初学阶段建议启用 error_reporting(E_ALL);,让所有警告/错误显式暴露。

遵循以上规范,即可安全、可靠地在 PHP 中持久化 JSON 数据,远离 undefined variable 和 not callable 类错误。

text=ZqhQzanResources