patrikbelis patrikbelis - 7 months ago 23
PHP Question

Cannot redeclare show404()

Fatal error: Cannot redeclare show404() (previously declared in /Users/patrikbelis/www/todo/_inc/config.php:33) in /Users/patrikbelis/www/todo/_inc/config.php on line 36


Here's function that I also tried to rename for etc. mine404pagetoshow() but still not working

Also I searched in project for functions but have only 3... in config, delete, edit.php

code in config.php

<?php

ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

// require stuff
require_once 'vendor/autoload.php';




// global variables
$base_url = 'http://localhost:8000/todo';


// connect to db
$database = new medoo([
'database_type' => 'mysql',
'database_name' => 'todoapp',
'server' => 'localhost',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',

]);

// global functions


function show404()
{
header("HTTP/1.0 404 NOT FOUND");
include_once "404.php";
die();
}


function get_item()
{


// if we dont have id or its empty
if ( ! isset($_GET['id']) || empty($_GET['id']) ) {
return false;
}

global $database;

$item = $database->get("items", "text", [
"id" => $_GET['id']
]);

if ( ! $item ) {
return false;
}

return $item;

}

?>


and that's how I'm loading it to php file - code in delete.php, edit.php

<?php
require_once '_inc/config.php';
$item = get_item();
if ( ! $item ) show404();
?>


404.php

<?php

include_once "partials/header.php"

?>

<div class="page-header">
<h1><a href="/todo" style="color:black">404</a></h1>
</div>

<p>
_not found
</p>

<?php include_once "partials/footer.php" ?>


Trying to fix it for a while and also I'm reading some questions here and on google but can't find solution

Answer

Fatal error: Cannot redeclare show404() (previously declared in /Users/patrikbelis/www/todo/_inc/config.php:33) in /Users/patrikbelis/www/todo/_inc/config.php on line 36

Seems like in 404.php file You've another show404() function

   33 function show404()
   34 {
   35     header("HTTP/1.0 404 NOT FOUND");
   36     include_once "404.php";
   37     die();
   38 }

I know it's not solution, but workaround:

  if(!function_exists('show404')) {
      function show404()
      {
          header("HTTP/1.0 404 NOT FOUND");
          include_once "404.php";
          exit(0);
      }
  }

Please check also files that You use in one script run, You'll find:

require '_inc/config.php';  // or include

that cause problem

Comments