“致命错误:无法重新声明 <function>"

2022-01-25 00:00:00 include php

我有一个函数(这正是它的显示方式,从我的文件顶部开始):

I have a function(this is exactly how it appears, from the top of my file):

<?php
//dirname(getcwd());
function generate_salt()
{
    $salt = '';

    for($i = 0; $i < 19; $i++)
    {
        $salt .= chr(rand(35, 126));
    }

    return $salt;
}
...

由于某种原因,我不断收到错误消息:

And for some reason, I keep getting the error:

致命错误:无法重新声明generate_salt() (之前声明过在/Applications/MAMP/htdocs/question-air/includes/functions.php:5)在/Applications/MAMP/htdocs/question-air/includes/functions.php在第 13 行

Fatal error: Cannot redeclare generate_salt() (previously declared in /Applications/MAMP/htdocs/question-air/includes/functions.php:5) in /Applications/MAMP/htdocs/question-air/includes/functions.php on line 13

我无法弄清楚为什么会发生这种错误或如何发生这种错误.有什么想法吗?

I cannot figure out why or how such an error could occur. Any ideas?

推荐答案

这个错误说明你的函数已经定义了;这可能意味着:

This errors says your function is already defined ; which can mean :

  • 您在两个文件中定义了相同的函数
  • 或者你在同一个文件的两个地方定义了相同的函数
  • 或者定义你的函数的文件被包含了两次(所以,函数似乎被定义了两次)

为了帮助解决第三点,一个解决方案是使用 include_once 而不是 include 包含您的 functions.php 文件时 -- 所以它不能被多次包含.

To help with the third point, a solution would be to use include_once instead of include when including your functions.php file -- so it cannot be included more than once.

相关文章