0

I'm trying to learn more about strings and arrays. I have this bit of code:

<?php
$states = "OH, VA, GA";
$arrayStates = explode(",", $states);
$exists = "GA";
print_r($arrayStates);

if (in_array($exists, $arrayStates)){
    echo "<br/>" . $exists . " " . "exists.";
} else {
    echo "<br/>" . $exists . " " . "doesn't exist.";
}
?>

According to my feeble mind, GA should exist in the array. If I put $exists = "OH", that works. But the screen is showing this:

Array ( [0] => OH [1] => VA [2] => GA ) 

GA doesn't exist.

What am I not understanding here?

3
  • Use var_dump for debugging. Commented Oct 31, 2013 at 4:03
  • When people edit my code - how do they make the colors show up? Commented Oct 31, 2013 at 4:05
  • Use the {} code tool to mark your code. Commented Oct 31, 2013 at 4:51

6 Answers 6

5

The array contains the string " GA" with a space as the first character. That's not equal to `"GA", which goesn't have a space.

You should either use explode(", "), $states) or call trim() on each element of the array:

$arrayStates = array_map('trim', explode(",", $states));
Sign up to request clarification or add additional context in comments.

3 Comments

Just thought I'd add to your explanation: his echo looks correct because HTML only displays one typed space unless you add &nbsp;
If the variable $states was a recordset, like $states = $row_getStates['states'];. Would there be a space too? Or would the explode not need a space?
Not sure I understand the question. The name of the variable doesn't matter. Are you asking if this would happen if you were reading the value from a database? In that case, it depends on what's in the DB.
1

You need to explode with a space after the comma.

$arrayStates = explode(", ", $states);

2 Comments

Oh. lol. Really quick, because I'm not clear - what is that second comma? I'm not really sure what it's purpose is.
@SherwoodPro If we're talking about the same comma, it separates the arguments that you're passing to the explode function. You don't technically need a space after that comma, but it looks much nicer if you do.
1

you're splitting with , but your text has spaces, so after split you have:

Array ( [0] => OH [1] => _VA [2] => _GA )

you can either split by ,_ (replace underscore with space)

or you can trim all values after split, like:

foreach ($arrayStates as $k => $v) $arrayStates[$k] = trim($v);

Comments

1

That is because it is being divided by , so your array contents are :

Array
(
    [0] => OH
    [1] =>  VA
    [2] =>  GA
)

you need to do $arrayStates = explode(", ", $states);

Comments

1

In $arrayStates after applying explode(...) you have:

$arrayStates[0] stores "OH"
$arrayStates[1] stores " VA"
$arrayStates[2] stores " GA"

Note at index 2 the array is storing " GA" (note the space) instead of "GA" that is because in the explode function you are using ",". To get your code working as you want you should use in the explode function ", " (note the space)

Comments

0

The explode method splits the string on the comma "," ONLY and does not remove the whitespace. As a result you end up comparing "GA" (your $exists) to " GA" (inside of the array, note the whitespace) =]

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.