“致命错误:无法重新声明 <function>" [英] "Fatal error: Cannot redeclare <function>"

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

问题描述

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

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.

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

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