“致命错误:无法重新声明<函数>" [英] &quot;Fatal error: Cannot redeclare &lt;function&gt;&quot;

查看:40
本文介绍了“致命错误:无法重新声明<函数>"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 而不是 includecode> 当包含你的 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.

这篇关于“致命错误:无法重新声明<函数>"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆