PHP,变量未定义

I am trying to make sort of a captcha verification and one of my variables is not defined. Here is my code for the captcha generator:

<?php
    $num = rand(1,3);

    switch($num) {
        case 1:
            $img = "";
            $answer = "nov 18th";
            break;
        case 2:
            $img = "";
            $answer = "jan 19th";
            break;
        case 3:
            img = "";
            $answer = "school bad";
            break;
    }
?>

Now here is my code for the script that checks whether or not the captcha input was entered correctly:

<?php
    ini_set('display_errors',1);

    print "<br /><br />";

    $captcha = $_POST['captcha-verif'];

    if ($captcha == $answer) {
        header('Location: recommend-action.php');
    }
    else {
        print "Incorrect Captcha. <a href='music.php'>Try Again</a>";
    }
?>

I keep getting this message:

Notice: Undefined variable: answer in /var/www/tts/recommend-verify-captcha.php on line 9

You don't declare answer variable in recommend-verify-captcha.php so, this variable is not existed.

Based on your code, I see you want answer is result from

<?php
$num = rand(1,3);

switch($num) {
    case 1:
        $img = "";
        $answer = "nov 18th";
        break;
    case 2:
        $img = "";
        $answer = "jan 19th";
        break;
    case 3:
        $img = "";
        $answer = "school bad";
        break;
}?>

So you can do like that

function getCapcha(){
    $num = rand(1,3);

    switch($num) {
        case 1:
            $img = "";
            $answer = "nov 18th";
            break;
        case 2:
            $img = "";
            $answer = "jan 19th";
            break;
        case 3:
            $img = "";
            $answer = "school bad";
            break;
    }
}

Then use this function in your recommend-verify-captcha.php file , maybe like as

<?php
    ini_set('display_errors',1);

    print "<br /><br />";

    $captcha = $_POST['captcha-verif'];
    $answer = getCapcha();
    if ($captcha == $answer) {
        header('Location: recommend-action.php');
    }
    else {
        print "Incorrect Captcha. <a href='music.php'>Try Again</a>";
    }
?>

You need a session variable:

<?php
    // start session here
    session_start();
    $num = rand(1,3);

    switch($num) {
        case 1:
            $img = "";
            // store answer in session variables. 
            $_SESSION['answer'] = "nov 18th";
            break;
        case 2:
            $img = "";
            $_SESSION['answer']  = "jan 19th";
            break;
        case 3:
            $img = "";
            $_SESSION['answer']  = "school bad";
            break;
    }
?>

In your second snippet make sure to start the session again.

<?php
    // restart session
    session_start();
    ini_set('display_errors',1);

    print "<br /><br />";

    $captcha = $_POST['captcha-verif'];

    // compare $captcha against answer stored in the session
    if ($captcha == $_SESSION['answer']) {
        header('Location: recommend-action.php');
    }
    else {
        print "Incorrect Captcha. <a href='music.php'>Try Again</a>";
    }
?>