Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP logical coding style
    text
    copied!<p>I have to modify some parts of a large PHP application. The different parts were written, of course, by different people (mostly interns). After looking through the code, I found that there were 2 styles of coding used by the other developers:</p> <ul> <li>The 'PHP is the glue of the Internet' style, mixing html and php, ex.:</li> </ul> <p>[snip]</p> <pre><code>&lt;tr class="ds_subsubhead_2"&gt; &lt;td colspan="21" align="left"&gt;&amp;nbsp;A&amp;nbsp;&lt;select name="nb_linge" onChange="MM_jumpMenu('parent',this,0)" style="vertical-align:middle"&gt; &lt;option value="&lt;?=get('index.php',$orgurl,'nb_ligne=','22','23','9999') ?&gt;" &lt;? if($messagesParPage == '9999') { ?&gt;selected="selected"&lt;? } ?&gt;&gt;Tous&lt;/option&gt; &lt;option value="&lt;?=get('index.php',$orgurl,'nb_ligne=','22','23','25') ?&gt;" &lt;? if($messagesParPage =='25') { ?&gt;selected="selected"&lt;? } ?&gt;&gt;25&lt;/option&gt; &lt;option value="&lt;?=get('index.php',$orgurl,'nb_ligne=','22','23','50') ?&gt;" &lt;? if($messagesParPage =='50') { ?&gt;selected="selected"&lt;? } ?&gt;&gt;50&lt;/option&gt; &lt;option value="&lt;?=get('index.php',$orgurl,'nb_ligne=','22','23','75') ?&gt;" &lt;? if($messagesParPage =='75') { ?&gt;selected="selected"&lt;? } ?&gt;&gt;75&lt;/option&gt; </code></pre> <p></p> <p>[snip] or</p> <pre><code>&lt;td &lt;? if((isset($_GET['t1']))&amp;&amp;($_GET['t2']!='ALL')) { ?&gt;bgcolor="#0099FF"&lt;? } ?&gt;&gt;&lt;/td&gt; &lt;td &lt;? if((isset($_GET['t3']))&amp;&amp;($_GET['t4']!='ALL')) { ?&gt;bgcolor="#0099FF"&lt;? } ?&gt;&gt;&lt;/td&gt; &lt;td &lt;? if((isset($_GET['t5']))&amp;&amp;($_GET['t6']!='ALL')) { ?&gt;bgcolor="#0099FF"&lt;? } ?&gt;&gt;&lt;/td&gt; </code></pre> <p>[snip] or even</p> <pre><code>&lt;script type="text/javascript" src="&lt;?=$_SESSION["path"]?&gt;lib/js/ajax.js"&gt;&lt;/script&gt; </code></pre> <p>[snip]</p> <ul> <li>... and the more procedural way, ex.:</li> </ul> <p>[snip]</p> <pre><code> $output .= '&lt;td valign="top"&gt;&lt;form name="form5" method="GET" action=""&gt;&lt;select name="m" onchange="this.form.submit()"&gt;'; if ( empty($_GET['p']) ) $output .= '&lt;option value=" "&gt;All&lt;/option&gt;'; else $output .= '&lt;option value='.$_GET['m'].'&gt;'.$_GET['m'].'&lt;/option&gt;'; $query = "SELECT DISTINCT maoie FROM ".$BD."site"; $res = mysql_query($query); while ( $row = mysql_fetch_assoc($res) ) { if( !empty($row['maoie']) ) $output .= '&lt;option value="'.$row['maoie'].'"&gt;'.$row['maoie'].'&lt;/option&gt;'; } $output .= '&lt;/select&gt;&lt;/form&gt;&lt;/td&gt;'; $output .= add_more_stuff(); echo $output; </code></pre> <p>Now, I'm not completely sure that this is a <em>more</em> procedural way to do things, but at least it is different from the previous one. Which one, do you think, is <em>generally</em> better? </p> <p>I, personally, dont't like 'the glue of the Internet' style.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload