1

I'm trying to teach myself PHP. My current exercise combines a form (not included in the code, but it works) that requires the user to enter the name of a city. The loop and the if statement compare the entry with an array of state capitals to return an answer that states whether that city is a state capital or not.

If I leave out the elseif part, the code runs ok, but I have no alternative when the user has entered a city that is not in the array. But with the elseif, the first part of the loop doesn't execute. For example, if I enter "Albany" without the elseif, I get "Albany is the capital of New York." But if I enter it with the elseif statement, it runs the loop until it finds "New York" and it prints "Albany is the capital of New York."

I've googled this, and I've read the books on PHP that I have. And I also know that I'm making a very basic mistake. Any guidance would be greatly appreciated.

for ($i = 0 ; $i < count($stateCapitalNames); $i++)

if ($enteredCity == $stateCapitalNames[$i]) {

print "<p>$enteredCity is the capital of <b>$stateNames[$i]</b>. </p>";


} elseif ($enteredCity != $stateCapitalNames[$i]){

print "<p>$enteredCity is not the capital of a state.</p>";

}

?>
1
  • 1
    On a sidenote, you do not need the else if, you can use else since $enteredCity != $stateCapitalNames[$i] is always true at this point. Commented Jul 8, 2013 at 1:23

2 Answers 2

7

You can use break to leave the for loop.

You should look at array_search to find the index you are looking for. array_search returns false if the capital does not exist.

For instance

$i = array_search($enteredCity, $stateCapitalNames);
if($i !== false)
{
    echo "<p>$enteredCity is the capital of <b>",$stateNames[$i],"</b>. </p>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Didn't know array_search existed. I like this +1 for a nifty tool.
This is the approach I would take, but if you were trying to learn how to iterate over the array and quit when you found what you are looking for, then a break is what you want for that.
2

You are missing your brackets in your for loop. I'm surprised the elseif is the culprit and that the code doesn't fail anyways. But here is what I would do, errors aside:

$correct = false;

for ($i = 0 ; $i < count($stateCapitalNames); $i++){
    if ($enteredCity == $stateCapitalNames[$i]) {
         $correct = true;
         $stateNames = $stateNames[$i]; // Updated $stateNames variable

         break;
    }
}

//You can check $correct here...
if($correct){
    print "<p>$enteredCity is the capital of <b>$stateNames[$i]</b>. </p>"; /*Removed [$i] from  $stateNames. For some reason, $stateNames[$i] wasn't updating outside the loop, but now it is. 
}

This way, no matter what, until the code finds a correct answer, the user is wronge. Once it finds the right answer, it sets it as correct and exits the loop by setting $i to the length of the array.

5 Comments

While it is bad practice (close to criminal) to omit brackets, it should still work since the entire if-else is considered one statement.
I tried this, but now the loop is not going through the states array.
@apartridge There are too many "bad practice"s on SO today: globals, references, singletons, echo heredoc / nowdoc, goto, etc... And, yeah, how many of us using one or two or all of them? It is nod "bad" while it is not in a curved hands.
I don't understand all of what you are trying to say. But bad practices are bad for a reason, they tend to cause unneccessary problems. One-liner statements could avoid using brackets, but for multi-lined statements you are bound to get into trouble.
@apartridge I didn't test the code; it was meant as a starting point. I don't see any errors at a glance but feel free to update it if you have a fix. You'll note that I didn't define the variables that were implied as defined by the OP's script (statecapitalnames, etc).

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.