2016-11-29 59 views
0

值下面的函數給我的下一個錯誤之後,當我試圖函數後呼應$oplossing功能不給變量

未定義的變量:oplossing

我試過多次事情,但不知何故,它不會從函數內部的變量的值到PHP文檔的其餘部分。我對編程相當陌生,所以這可能很明顯,但我不知道如何做到這一點。我該如何解決?

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

回答

0

如果你的函數聲明前兩$waarde$oplossing,然後調用global $waarde, $oplossing自己的函數中。

這發生作爲自己的函數中的可變因素有本地範圍,而不是一個全球性

+0

它固定我有錯誤,但我怎麼再取功能外$ oplossing和新值$ getal? –

+0

你應該沒問題,只需按照通常在函數後面調用它們即可。你想在外部聲明的函數之外訪問任何變量,然後使其在內部全局化。 – Ryan

+0

只是返回它們:return ['oplossing'=> $ oplossing,'getal'=> $ getal]; – ahammar