1

My issue is that the dropdown is actually populating correctly but with only the first letter of that particular line.

I have the following in my file:

Monday

Tuesday

Wed

It is displaying as:

M

T

W

Any ideas?

My code:

<select id="playlist_wrongstyle" class="form-control"  style="visibility:visible; width:250px;">
<option selected="selected">Choose one</option>
<?php
  $returnedScheduleNamesArray = explode ("\n", file_get_contents('/srv/http/schedulenames'));

  array_pop($returnedScheduleNamesArray); //remove empty last line

  foreach($returnedScheduleNamesArray as $name) 
  {
?>
      <option value="<?=$name['name']?>"><?=$name['name']?></option>
<?php
  }
?>
</select> 
3
  • 4
    Try just: <?= $name ?> Commented Aug 30, 2016 at 14:21
  • $name is a string? Commented Aug 30, 2016 at 14:21
  • @MagnusEriksson Perfect! Thanks a million :) Commented Aug 30, 2016 at 14:22

3 Answers 3

5

$name is a string. You should just echo name: <?= $name >

The reason you are seeing a single letter:

  • String characters can be accessed using array bracket syntax:

    $name = 'Monday';
    $name[0] === 'M'; // true
    
  • PHP is coercing your string 'name' into an integer, and this evaluates to 0:

    (int)'name' === 0; // true
    
  • So, $name['any string'] === $name[0]; // true

Sign up to request clarification or add additional context in comments.

Comments

4

This is basically what's happening with $name['name']:

foreach (['Monday', 'Tuesday'] as $name) {
    echo $name['nonexistent'];
}

If you adjust your error reporting level, you'll see

Warning: Illegal string offset 'nonexistent'

When PHP sees you use ['something'] after a string, it thinks you're trying to access a specific character within it (which you can do, but only with numeric indexes). But, since PHP expects an integer there, when you reference an illegal string offset, it gets converted to an integer, so you'll probably get index zero, the first letter, unless your illegal string offset starts with something that will convert to nonzero. (e.g. $day['3illegal'] gets you "d" and "s".)

4 Comments

Actually, you get index 0 because (int)'some string' evaluates to 0
It's not just because PHP loves us? ;)
I guess it's a little bit of both. :)
You're right though, I probably shouldn't joke about such things. I'll edit.
3

Ooops, you've found the PHP magic trick. PRESTO! Strings are arrays!

         $name ='Bob';// thats a string
         echo $name['0']; // prints B, like an array
         echo $name['1']; // prints o, like an array

Strings variables are actually arrays, but in your case, you don't want that.. so

<option value="<?=$name?>"><?=$name?></option>

2 Comments

That'll print "o" - strings are zero-indexed just like arrays.
Well it was magic when I accidentally discovered it back in the year 2000sumthin , and I'll test but I thought they were zero index, eg; $name['0'] was the whole string, 1 was the first char.. anyways, still magic, just unimpressive magic :p

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.