尝试回显数组数据时出错

<?php
    $greeting = "Hello";
    $place = "World";
    $num = 3;
    $data = array(
        0 => "zero", 
        1 => "one"
    );
    echo "<pre>";
    echo $greeting;
    echo "

";
    echo '$greeting' . "$place";
    echo "

";
    echo $num * 8;
    echo "

";
    echo "Greeting
World";
    echo "

";
    echo $data['$num'].'is a 
 number';
    echo "

";
    echo "It's" . "$data[1]" . "small" . "$place";
    echo "

";
    echo $data;
    echo "

";
    echo substr($hello . " and good luck! ", 3, 15);
    echo "

";
    echo "<pre>";
?>

Here is PHP code that I received for testing purposes. I copied the code word for word, but for some reason I get the following errors:

Notice: Undefined index: $num in C:\xampp\htdocs\test.php on line 18

Notice: Array to string conversion in C:\xampp\htdocs\test.php on line 22

Notice: Undefined variable: hello in C:\xampp\htdocs\test.php on line 24

This was a question given on a midterm where I was supposed to interpret the output. It is possible that these errors were made on purpose, but I don't understand why. Could someone please explain why these errors are taking place?

Notice: Undefined index: $num in C:\xampp\htdocs\test.php on line 18

The single quotes makes it a string literal and the variable is not interpolated:

echo $data[$num].'is a 
 number';

Notice: Array to string conversion in C:\xampp\htdocs\test.php on line 22

$data is an array, not a string. So you can't echo it out like a string:

print_r($data);

Notice: Undefined variable: hello in C:\xampp\htdocs\test.php on line 24

You never declare $hello so it obviously doesn't exist and you can't use it. Either declare it as an empty string or remove the code that attempts to use it.

$hello = '';

'$num' and "$num" is not the same in PHP. In '$num' PHP trys not to find a variable.

The first error references to the string "$num" not being in the array:

array('$num' => 0)

This should be $data[$num].

The to string conversion happends on

echo $data;

Your trying to output an array, that's why it states array to string conversion.

Then lastly, the $hello variable was never defined!

I added some comments next to the lines to be corrected.

<?php
    $greeting = "Hello";
    $place = "World";
    $num = 0; //It should be 1 or 0 because you are using it as index for $data array
    $data = array( // or you can use $data = array("zero","one");
        0 => "zero", 
        1 => "one"
    );
    echo "<pre>";
    echo $greeting;
    echo "

";
    echo $greeting . "$place"; //Single quotes display the text as it is, so echo '$variable' displays $variable, however echo "$variable" display the content of variable.
    echo "

";
    echo $num * 8;
    echo "

";
    echo "Greeting
World";
    echo "

";
    echo $data[$num].'is a 
 number'; //$num is a variable, you should not put it in singe quotes
    echo "

";
    echo "It's" . $data[1] . "small" . $place; //No need for the double quotaions
    echo "

";
    print_r($data); //You can't echo an array, you can use print_r instead
    echo "

";
    echo substr($greeting . " and good luck! ", 3, 15); //$hello is not defined anywhere
    echo "

";
    echo "<pre>";
?>