Undefined variable problem with PHP function

前端 未结 5 1982
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 08:06

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



        
5条回答
  •  时光取名叫无心
    2020-11-27 08:43

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

    See the PHP manual page on variable scope for more information.

    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 $pera as an argument to your function as follows:

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

提交回复
热议问题