在 if 语句中使用函数时出现“致命错误:调用未定义函数"? [英] Getting 'Fatal error: Call to undefined function' when using a function inside an if statement?

查看:59
本文介绍了在 if 语句中使用函数时出现“致命错误:调用未定义函数"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我刚刚完成了一个用于验证表单上的 firstname 字段的函数.

Ok, so I just finished off a function for validating the firstname field on a form.

此功能独立工作正常.

但由于我想让这个功能可以在多个网站上重复使用,所以我添加了一个 if 语句来确定是否使用它.下面的代码解释了这一点:

But since I want to make this function re-usable for more than one website, I added an if statement for whether or not to use it. The following code explain this:

相关PHP代码:

//Specify what form elements need validating:
$validateFirstname = true;

//array to store error messages
$mistakes = array();

if ($validateFirstname=true) {
    //Call first name validation function
    $firstname = '';
    if (!empty($_POST['firstname'])) {
        $firstname = mysql_real_escape_string(stripslashes(trim($_POST['firstname'])));
    }
    $firstname = validFirstname($firstname);
    if ($firstname === '') {
        $mistakes[] = 'Your first name is either empty or Enter only ALPHABET characters.';
    }
    function validFirstname($firstname) {
        if (!ctype_alpha(str_replace(' ', '', $firstname))) {
            return '';
        } else {
            return $firstname;
        }
    }
}

所以没有这个 if ($validateFirstname=true) 代码运行良好,但我添加它的那一刻;我收到以下错误消息:

So without this if ($validateFirstname=true) the code runs fine, but the moment I add it; I get the following error message:

Fatal error: Call to undefined function validFirstname()

您在 PHP 中根本无法使用 if 语句中的函数吗?我刚开始以这种方式使用它们.

Are you not able to use functions in if statements at all in PHP? I'm fairly new to using them in this way.

推荐答案

条件函数(在条件中定义的函数)必须在引用之前定义.以下是手册内容:

Conditional functions (functions defined inside the conditions) must be defined before they are referred. Here's what manual says:

函数在被引用之前不需要定义,除非一个函数是有条件地定义的,如两个示例所示下面.

Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.

当一个函数以有条件的方式定义时,例如两个示例显示.它的定义必须在之前处理调用.

When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called.

所以如果你想那样使用它,你应该把它放在 if 条件的开头或条件之外.

So if you want to use it that way, you should put it either at the beginning of the if condition or outside the condition.

// Either:
if ($validateFirstname==true) {
    function validFirstname($firstname) {}
}

// Or, and I'd rather do it this way, because function is
// created during "compilation" phase
function validFirstname($firstname) {}
if ($validateFirstname==true) {
    // ...
}

也不将该函数(即使在条件内创建)推送到全局范围:

Also not that function (even if created inside the condition) is pushed to global scope:

PHP 中的所有函数和类都具有全局作用域 - 即使它们是在函数内部定义的,也可以在函数外部调用,反之亦然.

All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.

因此,一旦代码被评估,它是在条件内部声明还是有意在全局范围内声明都无关紧要.

So once code is evaluated it doesn't matter if it's declared inside condition or intentionally in global scope.

这篇关于在 if 语句中使用函数时出现“致命错误:调用未定义函数"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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