user1730357
user1730357

Reputation: 355

Errors when trying to echo array data

<?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?

Upvotes: 0

Views: 76

Answers (4)

CMPS
CMPS

Reputation: 7769

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>";
?>

Upvotes: 0

wtfzdotnet
wtfzdotnet

Reputation: 1062

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!

Upvotes: 0

AbcAeffchen
AbcAeffchen

Reputation: 14987

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

Upvotes: 0

John Conde
John Conde

Reputation: 219824

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 = '';

Upvotes: 1

Related Questions