Need help understanding JOIN()

Hi folks,

I have some code from a book that makes a select list out of an array:

$cities = array('Cork', 'Dublin', 'Galway', 'Limerick', 'Waterford');

echo '<select name="city"><option>' . join('</option><option>',$cities) . '</select>';

My confusion comes from this line:

join('</option><option>',$cities)

From what I understand from the PHP manual, the first argument in the join function is the ‘glue’ that separates each item in the array. With the first opening <option> tag before the join, the statement would result in something like this:

Cork</option><option>
Dublin</option><option>
Galway</option><option>
Limerick</option><option>
Waterford</option><option>

What I don’t understand is that last item, which is followed with both a closing and an opening <option> tag (like the others), but in the rendered HTML, it gets the closing </option> tag but that last opening <option> tag is not there. That’s perfect, and it makes it work, but I don’t understand why that last tag doesn’t show up. If each item in the array gets the “glue”, why does the last one only get the closing </option> tag? And why does the last one get anything at all? Usually with implode() or join() the last item in the array doesn’t get any “glue” following it. Can anyone please help me understand? It’s driving me nuts…

Hi,

are you sure of your code ?

isn’t it

echo ‘<select name=“city”><option>’ . join’</option><option>',$cities) . ‘</option></select>’;

Take a better look at the output, the last </option> isn’t there:


<select name="city">
  <option>Cork</option>
  <option>Dublin</option>
  <option>Galway</option>
  <option>Limerick</option>
  <option>Waterford    <-- Note the absence of </option> here
</select>

And the statement doesn’t result in this:


Cork</option><option>
Dublin</option><option>
Galway</option><option>
Limerick</option><option>
Waterford</option><option>

But this:


Cork</option><option>
Dublin</option><option>
Galway</option><option>
Limerick</option><option>
Waterford

It only puts the glue between items, not before the first element and not after the last element.

The correct way to do it would be:


echo '<select name="city"><option>' . join('</option><option>',$cities) . '[COLOR="Blue"]</option>[/COLOR]</select>';

Does that make sense? :slight_smile:

Yes, thanks very much for you help in all this. This is only the beginning of the book so I’ve got my work cut out for me. Glad to have these forums to get answers from those more experienced. Much appreciated. :slight_smile:

If there is just one statement to perform if the if statement validates it’s okay to leave out the curly brackets


if ($something)
  doSomething(); // is only executed if $something evaluates to true

if ($somethingElse)
  doSomething();  // is only executed if $somethingElse evaluates to true
  doSomethingElse(); // is *always* executed, regardless of the value of $somethingElse and the indentation that might suggest otherwise


if ($somethingElse) {
  doSomething();  // is only executed if $somethingElse evaluates to true
  doSomethingElse(); // is only executed if $somethingElse evaluates to true
}

Does that help? :slight_smile:

Thanks again. I didn’t know you could do such a thing with echo commands. I am constantly learning!

The book author has a different way of writing his PHP than I’m used to. For example, when he writes an “if” statement, he leaves out the curly braces, like this:

if(!$cities)echo 'please choose a country first';

Is that an accepted way of doing things? Seems a little sloppy or lazy to me, but that’s just because I’m used to the other way.

The commas in that example are not for concatenation, it’s just to provide several arguments to the echo command at once.
Some argue that doing that instead of using real concatenation using the . is slightly faster than concatenation, although the difference is in my opinion negligible.
I do find it prettier to use commas instead of dots, but that’s a personal preference :slight_smile:

I see, thanks for explaining that. While I have your attention :), the actual code as written in the book has commas as concatenation instead of periods. Is this some form of acceptable alternate PHP syntax? The code still works with the commas. Here it is:

echo '<select name="city"><option>' , join('</option><option>',$cities) , '</select>'; 

Depending on the doctype, browsers don’t raise an error when you forget to close a tag somewhere; it’ll just assume you forgot to close it and make a best guess as to what was meant. For <options> it most likely assumes they should be closed and render it as if they were. That doesn’t mean it’s okay to leave out stuff, it’s not! Different browsers may interpret errors differently and while one browsers guesses how the fix it correctly another browser may not.
If you were to use XHTML using content-type text/xml than you would get an error and the browser won’t show your page.

Thanks for taking the time to reply folks.

ScallioXTX - I see what you mean. The last </option> is not there in the output, yet “Waterford” still makes it into the select list. How is that possible when it doesn’t have a closing </option> tag?

I agree with you both on the correct way of writing the code. This is taken from a book called “jQuery 1.3 with PHP” which is supposed to be written for experienced PHP programmers, so I assumed they knew what they’re doing. The code as written does the job, but I am wondering why.