Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查PHP中是否存在函数_Php - Fatal编程技术网

检查PHP中是否存在函数

检查PHP中是否存在函数,php,Php,我想检查函数是否存在。我正在使用函数_exists()进行检查 $func = $_GET['function']; $func(); if(function_exists('func')) { echo "Exists"; } else { echo "Invalid Function name called"; } 好的,现在我有一个名为register的函数,我这样调用它: $allowed_functions = ['function1', 'function2', '

我想检查函数是否存在。我正在使用函数_exists()进行检查

$func = $_GET['function'];
$func();
if(function_exists('func'))
{
    echo "Exists";
}
else
{
    echo "Invalid Function name called";
}
好的,现在我有一个名为
register
的函数,我这样调用它:

$allowed_functions = ['function1', 'function2', 'function3'];

if (!empty($_GET['function']) && in_array($_GET['function'], $allowed_functions)) {
    call_user_func($_GET['function']);
}
else {
    trigger_error("Invalid function");
}
www.example.com/index.php?function=register
好的,它工作正常,因为这个函数确实存在。 但是如果

www.example.com/index.php?function=registerssss
它给了我这个错误:

调用未定义的函数registersss()


我想打印我的自定义错误,因为我已经张贴在上面。有解决方案吗?

在检查函数是否存在之前,您正在调用该函数

$func = $_GET['function'];

if (function_exists($func)) {
    echo "Exists";
    $func();
} else {
    echo "Invalid Function name called";
}

这里有一个解决方案。我修改了您的代码,使其能够工作:

<?php
  if (isset($_GET['function']) && function_exists($_GET['function'])) {
    $func = $_GET['function']; // Set the URL variable to a regular PHP var
    $func();                   // Do the function
    echo "Exists and ran";     // Let the user know the function exists and it was ran
  } 
  else {
    echo "Invalid function name called"; // Function does not exists, so output custom error message
  }
?>


您获取的
$\u的网址

index.php?function=rrr ***FALSE***

index.php?function=test ***TRUE***
这在以下方面效果良好:

function test() {
    echo "test";
}

$foo = $_GET['function'];

// Check $_GET has data and function exists 
if (isset($_GET['function']) && function_exists($foo)) {
    echo "True";
} else {
    echo "False";
}

功能\u存在
不是此处作业的正确工具。如何通过将内置函数附加到URL来防止用户执行内置函数

而是使用一个开关,只包含您已定义并希望允许用户执行的功能:

switch ($_GET['function']) {
    case 'function1':
        function1();
        break;
    case 'function2':
        function2();
        break;
    case 'function3':
        function3();
        break;
    default:
        trigger_error("Invalid function");
}
或者您可以改为使用数组,如下所示:

$allowed_functions = ['function1', 'function2', 'function3'];

if (!empty($_GET['function']) && in_array($_GET['function'], $allowed_functions)) {
    call_user_func($_GET['function']);
}
else {
    trigger_error("Invalid function");
}

只有在存在问题时才打电话不管你想解决什么问题,不要这样解决。这只会助长一种坏习惯。好吧!你很严格,但是idk什么,但是现在当我更改我的www.example.com/index.php?function=register时,它会将我的部分推到else,是的,函数register确实存在!注册是类的一部分吗?该函数包含在该文件中?需要进行解释。它为什么有效?请回复,而不是在这里的评论。