php PHP函数的未定义变量问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6058781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:14:35  来源:igfitidea点击:

Undefined variable problem with PHP function

phpfunctionvariablesundefined

提问by Mentalhead

I'm a PHP newbie, so I have a minor problem functions. I have this line of code:

我是 PHP 新手,所以我有一个小问题函数。我有这行代码:

<?php
$ime=$_POST["ime"];
$prezime=$_POST["prezime"];
$pera="string";
if (empty($ime)||empty($prezime)){
    echo "Ne radi, vrati se nazad i unesi nesto!";
}
function provera($prom){
    if (preg_match("/[0-9\,\.\?\>\.<\"\'\:\;\[\]\}\{\/\!\\@\#$\%\^\&\*\(\)\-\_\=\+\`[:space:]]/",$prom)){
        echo "Nepravilan unos imena ili prezimina!";
        echo $pera;
        }
}
provera($ime);
provera($prezime);
?>

Anyway, when I try this code I always get an error message saying that there's a error on on line 11 (the bold part of the code) and no variable is echoed. I'm guessing that it gives me that error because my variable isn't defined inside of that function, but I need to define it outside of the function so is there a way to do this?

无论如何,当我尝试这段代码时,我总是收到一条错误消息,指出第 11 行(代码的粗体部分)有错误,并且没有回显变量。我猜它给了我那个错误,因为我的变量没有在该函数内部定义,但我需要在函数外部定义它,所以有没有办法做到这一点?

回答by John Parker

This is because you're using the $peravariable (which exists only in the global scope) inside a function.

这是因为您在$pera函数内使用了变量(仅存在于全局作用域中)。

See the PHP manual page on variable scopefor more information.

有关更多信息,请参阅有关变量作用域PHP 手册页

You could fix this by adding global $pera;within your function, although this isn't a particularly elegant approach, as global variables are shunned for reasons too detailed to go into here. As such, it would be better to accept $peraas an argument to your function as follows:

您可以通过global $pera;在您的函数中添加来解决此问题,尽管这不是一种特别优雅的方法,因为由于太详细的原因而避免使用全局变量。因此,最好接受$pera函数的参数,如下所示:

function provera($prom, $pera){
    if (preg_match("/[0-9\,\.\?\>\.<\"\'\:\;\[\]\}\{\/\!\\@\#$\%\^\&\*\(\)\-\_\=\+\`[:space:]]/",$prom)){
        echo "Nepravilan unos imena ili prezimina!";
        echo $pera;
        }
}

回答by AllisonC

In your function function provera($prom) add a line that says

在您的函数 functionprovera($prom) 中添加一行表示

global $pera;

回答by WINGLEUNG CHOI

If your PHP version is on 5.3 or later versions, closure can be applied.

如果您的 PHP 版本是 5.3 或更高版本,则可以应用闭包。

Closures may also inherit variables from the parent scope.

Closures may also inherit variables from the parent scope.

useis the php syntax to implement closure.

use是实现闭包的php语法。

ref: Anonymous functions

参考:匿名函数

    <?php
    // $ime=$_POST["ime"];
    // $prezime=$_POST["prezime"];
    $pera="string";
    $prezime = "Ne radi, vrati se nazad i unesi nesto!";
    // if (empty($ime)||empty($prezime)){
    //     echo "Ne radi, vrati se nazad i unesi nesto!";
    // }
    $provera = function ($prom) use ($pera) {
        if (preg_match("/[0-9\,\.\?\>\.<\"\'\:\;\[\]\}\{\/\!\\@\#$\%\^\&\*\(\)\-\_\=\+\`[:space:]]/",$prom)){
            echo "Nepravilan unos imena ili prezimina!";
            echo $pera;
        }
    };

    // $provera($ime);
    $provera($prezime);

回答by lvictorino

It sounds like you have nothing set in your $pera variable. If you have to define a variable outside a function, try passing its value as argument to your function.

听起来您的 $pera 变量中没有设置任何内容。如果必须在函数外部定义变量,请尝试将其值作为参数传递给函数。

function echoMyVar( $myVar )
{
   echo $myVar;
}


$p = "toto";
echoMyVar($p);

回答by JohnP

You can't use $perainside the method like that because it's not defined inside the method scope.

你不能$pera像那样在方法内部使用,因为它没有在方法范围内定义。

If you want to use the method, pass it as a parameter.

如果要使用该方法,请将其作为参数传递。

function provera($prom, $pera){ //passed as a param
    if (preg_match("/[0-9\,\.\?\>\.<\"\'\:\;\[\]\}\{\/\!\\@\#$\%\^\&\*\(\)\-    \_\=\+\`[:space:]]/",$prom)){
        echo "Nepravilan unos imena ili prezimina!";
    echo $pera;
}