Gijs
Gijs

Reputation: 149

Function does not give value of variables

The following function gives me the next error afterwards when I tried to echo $oplossing after the function:

Undefined variable: oplossing

I've tried multiple things but somehow it does not take the valua of the variables from inside the function to the rest of the php document. I'm quite new to programming so this might be obvious but I don't know how to do this. How do I fix this?

 $moeilijkheidsgraad = $_POST["moeilijkheidsgraad"];
$waarde = $_POST["waarde"];

function WaardeGetal ($moeilijkheidsgraad, $waarde)
{
if ( $moeilijkheidsgraad == "makkelijk" ){      //makkelijk
    if ( $waarde == "dechex" ){                 //van decimaal naar hexadecimaal
        $getal = rand(0,32);                    //willekeurig getal tussen 0 en 50
        $oplossing = dechex($getal);
        $oplossing = strtoupper($oplossing);    //omzetten van decimale getal naar hexadecimaal getal
    }                           


    elseif ( $waarde == "hexdec") {             //hexadecimaal naar decimaal
        $oplossing = rand(0,32);                //willekeurig getal tussen 0 en 50
        $getal = dechex($oplossing);
        $getal = strtoupper($getal);            //omzetten van decimale getal naar hexadecimaal getal
    }

    elseif ($waarde == "decbin") {
        $getal = rand(0,16);
        $oplossing = decbin($getal);
    }

    elseif ($waarde == "hexbin"){
        $getal = rand(0,16);
        $getal = dechex($getal);
        $oplossing = base_convert($getal,16,2);
        $getal = strtoupper($getal);
        }

    elseif ($waarde == "binhex" ) {
        $oplossing = rand(0,16);
        $oplossing = dechex($oplossing);
        $getal = base_convert($oplossing,16,2);
        $oplossing = strtoupper($oplossing);
        $aantal = 0;
        }

    else {
        $oplossing = rand(0,16);
        $getal = decbin($oplossing);
    }
    }

else {                                          //moeilijk
    if ( $waarde == "dechex" ){                 //van decimaal naar hexadecimaal
        $getal = rand(32,256);                  //willekeurig getal tussen 0 en 50
        $oplossing = dechex($getal);            //omzetten van decimale getal naar hexadecimaal getal
        $oplossing = strtoupper($oplossing);
        }

  elseif  ( $waarde =="hexdec" ){               //hexadecimaal naar decimaal
        $oplossing = rand(32,256);              //willekeurig getal tussen 0 en 50
        $getal = dechex($oplossing);            //omzetten van decimale getal naar hexadecimaal getal
        $getal = strtoupper($getal);    
    }

    elseif ($waarde == "decbin") {
        $getal = rand(16,256);
        $oplossing = decbin($getal);
    }

    elseif ($waarde == "hexbin"){
        $getal = rand(16,256);
        $getal = dechex($getal);
        $oplossing = base_convert($getal,16,2);
        $getal = strtoupper($getal);
        }

    elseif ($waarde == "binhex" ) {
        $oplossing = rand(16,256);
        $oplossing = dechex($oplossing);
        $getal = base_convert($oplossing,16,2);
        $oplossing = strtoupper($oplossing);
        }

    else {
        $oplossing = rand(16,256);
        $getal = decbin(oplossing);
    }
 }
 return
 }

WaardeGetal($moeilijkheidsgraad, $waarde);

Upvotes: 0

Views: 39

Answers (1)

Ryan
Ryan

Reputation: 531

If you declare both $waarde and $oplossing before your function and then call global $waarde, $oplossing inside your function.

This occured as the varibles inside your function had a local scope and not a global one

Upvotes: 1

Related Questions