php - Preferred formatting for this / best practice? -


Suppose I want to create a select box in PHP.

$ months = array ('Jan', 'Feb', 'March', 'Apr', 'May', 'Jun', 'Jul', 'Aug', September ' ,' October November December);

Option 1:

  & lt; Select name = "month" & gt; & Lt; Option value = "" & gt; Month & lt; / Options & gt; & Lt; Option value = "" & gt; ------- & lt; / Options & gt; & Lt ;? Php foreach ($ months as $ month = = $ month) {? & Gt; & Lt; Option value = "& lt ;? php echo $ monthNum + 1? & Gt;" & Gt; & Lt ;? Php echo $ month? & Gt; & Lt; / Options & gt; & Lt ;? Php}? & Gt; & Lt; / Select & gt;  

Option 2:

  & lt; Select name = "month" & gt; & Lt; Option value = "" & gt; Month & lt; / Options & gt; & Lt; Option value = "" & gt; ------- & lt; / Options & gt; & Lt ;? Php foreach (month (= $ month = = $ month) echo '& lt; Option value = "'. ($ MonthNum + 1). & Gt;'. $ Month '& lt; / Options & gt; I personally feel that making the code difficult to read and understand option 1, one can In the same time option 2 puts the screw indentation and more HTML into PHP.  

Which option do you like?

Edit: I do not have another template engine at the top of PHP I want to add, which is a template engine.

I think the second option is better Nki & lt; option value = "& lt ;? Php echo $ monthNum + 1? & Gt; "& Gt; Hard to understand and hard to understand and Imo also a bit incompatible (a tag in the tag.)

But honestly, use the more you like. Tip: Maybe you can use this syntax, which is sometimes difficult to understand & lt ;? php foreach () {? & Gt; ... & lt ?? php}? & Gt; Are: & lt ;? php foreach ():? & Gt; ... & lt ;? php endforeach;? & Gt; . Especially if you start forges in Forage in France If you do this syntax one Smaller can be easier to understand.


Comments