Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When adding content to json file php error occurs

I’m learning php and started following a youtube video for a project.

When i accept contents from my json file and code it to add new contents to it, an error occurs.

Error-

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Warning: Undefined variable $json_encode in /Users/montekkundan/Downloads/coding/php/newtodo.php on line 16

Fatal error: Uncaught Error: Value of type null is not callable in /Users/montekkundan/Downloads/coding/php/newtodo.php:16 Stack trace: #0 {main} thrown in /Users/montekkundan/Downloads/coding/php/newtodo.php on line 16

php code-

<?php
// echo "<pre>";
// var_dump($_POST);
// echo "</pre>";

$todoname = $_POST['todoname'] ?? "";
$todoname = trim($todoname);

if($todoname) {
  $json = file_get_contents('todo.json');
  $jsonArray = json_decode($json, true);
  $jsonArray[$todoname] = ['completed' => false];
  // echo "<pre>";
  // var_dump($jsonArray);
  // echo "</pre>";
 file_put_contents('todo.json', $json_encode($jsonArray, JSON_PRETTY_PRINT)) ;
}
?>

>Solution :

Try to change
file_put_contents('todo.json', $json_encode($jsonArray, JSON_PRETTY_PRINT));
to file_put_contents('todo.json', json_encode($jsonArray, JSON_PRETTY_PRINT));
that’s because you are trying to call json_encode variable, json_encode is not a variable.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading