Undefined variable problem with PHP function

39,411

Solution 1

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;
        }
}

Solution 2

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

Closures may also inherit variables from the parent scope.

use is the php syntax to implement closure.

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);

Solution 3

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

global $pera;

Solution 4

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.

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


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

Solution 5

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

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;
}
Share:
39,411
Mentalhead
Author by

Mentalhead

Web designer wannabe.

Updated on July 18, 2022

Comments

  • Mentalhead
    Mentalhead almost 2 years

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

    <?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?

  • Mentalhead
    Mentalhead almost 13 years
    Tried it before, and I'm three errors now 2x Missing argument 2 for provera() on line 14 and Undefined variable: pera on line 11
  • Mentalhead
    Mentalhead almost 13 years
    I tried using $pera as a function argument, but now I'm getting errors that my second argument for provera() is missing and that $pera is unidentified.
  • John Parker
    John Parker almost 13 years
    @Mentalhead - My apologies - when you call the function you need to supply the variable as a parameter. i.e.: provera($ime, $pera); and provera($prezime, $pera);. Hope this helps.
  • martinstoeckli
    martinstoeckli almost 13 years
    @Mentalhead - As middaparka already pointed out, you should call the function with 2 parameters provera($ime, $pera)
  • Mentalhead
    Mentalhead almost 13 years
    Thank you very much for clarifying this.