0

I'm really struggling with making this work, I have looked at guides but I can't seem to see the difference between mine and theirs, other than how the array is laid out.

<?php
$country = array('England' => "London", 'Scotland' => "Edinburgh", 'France' => "Paris");

foreach ($capitals as $country=>$capital) {
echo "The capital of $country is $capital";
}

?>

All I want it to do is say the country and its capital.

error code --

Notice: Undefined variable: capitals in C:\xampp\htdocs\foreach.php on line 4

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\foreach.php on line 4

Thanks in advance.

5
  • 1
    Well, where did you define $capitals? I don't see it anywhere. I guess you want to use $capitals = array(...); instead of $country = array(...);. Commented Dec 17, 2014 at 16:52
  • 1
    ericlippert.com/2014/03/05/how-to-debug-small-programs Commented Dec 17, 2014 at 16:54
  • stackoverflow.com/help/how-to-ask Commented Dec 17, 2014 at 16:58
  • You could at least include the example shown in the guide for comparison. Saying "I've been looking at guides" doesn't tell us which guide. Commented Dec 17, 2014 at 17:01
  • Which part of Undefined variable: capitals don't you understand? (that's a serious question, I'm not being condescending, just don't know how to phrase it better). It gives a pretty clear signal what is wrong, namely that you are trying to access a variable that doesn't exist. Commented Dec 17, 2014 at 17:06

2 Answers 2

5

Change your code to this:

(I change the array name from country to capitals)

<?php

    $capitals = array('England' => "London", 'Scotland' => "Edinburgh", 'France' => "Paris");

    foreach ($capitals as $country=>$capital) {
        echo "The capital of $country is $capital<br />";
    }

?>

Output:

The capital of England is London
The capital of Scotland is Edinburgh
The capital of France is Paris
Sign up to request clarification or add additional context in comments.

3 Comments

What did you change and why?
@FelixKling I change the array name from: country to capitals, because OP just named the array wrong.
Thanks I knew it would be a small change that fixed it
0

You're trying to loop through an array, $capitals, that doesn't exist. You have to loop through the array $country.

foreach ($country as $country=>$capital) {
    echo "The capital of $country is $capital";
}

4 Comments

Will there be any problems with naming the loop variable $country as well?
In testing it works just fine. It isn't good style, but it works @FelixKling
Only if you don't want to access the array after the loop: codepad.org/2KxU6ACH
True @FelixKling - my answer was more about looping through something that didn't exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.