0
<?php
    $greeting = "Hello";
    $place = "World";
    $num = 3;
    $data = array(
        0 => "zero", 
        1 => "one"
    );
    echo "<pre>";
    echo $greeting;
    echo "\n\n";
    echo '$greeting' . "$place";
    echo "\n\n";
    echo $num * 8;
    echo "\n\n";
    echo "Greeting\nWorld";
    echo "\n\n";
    echo $data['$num'].'is a \n number';
    echo "\n\n";
    echo "It's" . "$data[1]" . "small" . "$place";
    echo "\n\n";
    echo $data;
    echo "\n\n";
    echo substr($hello . " and good luck! ", 3, 15);
    echo "\n\n";
    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?

2
  • 2
    I'm guessing you're not doing well in the class. These errors are all pretty self-explanatory. Commented Apr 11, 2014 at 23:10
  • 1
    And yes, the errors are clearly intentional -- the whole point of the question was for you to figure out what caused the errors. Commented Apr 11, 2014 at 23:12

4 Answers 4

1

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 \n 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 = '';
Sign up to request clarification or add additional context in comments.

1 Comment

This was a midterm question. So this is copied directly from the test. It was not about whether it was correct and error free. I just had to trace the output.
0

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

Comments

0

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!

Comments

0

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 "\n\n";
    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 "\n\n";
    echo $num * 8;
    echo "\n\n";
    echo "Greeting\nWorld";
    echo "\n\n";
    echo $data[$num].'is a \n number'; //$num is a variable, you should not put it in singe quotes
    echo "\n\n";
    echo "It's" . $data[1] . "small" . $place; //No need for the double quotaions
    echo "\n\n";
    print_r($data); //You can't echo an array, you can use print_r instead
    echo "\n\n";
    echo substr($greeting . " and good luck! ", 3, 15); //$hello is not defined anywhere
    echo "\n\n";
    echo "<pre>";
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.