Sunday, July 18, 2010

Cleaning your HTML

What I see in the internet is that people don't know exactly how to organize their programing language inside the HTML tags.

People are using this:
$html = "
  • " . $var . "
"; echo $html; ?>


instead of:
  • <?php echo $var;?>


Another ordinary situation is this:
<table>
<?php
foreach($var as $k=>$v){
echo "<tr>";
echo "<td>".$v."</td>";
echo "</td>";
}
?>
</table>


and how to handle it:
<table>
<?php foreach($var as $k=>$v):?>
<tr>
<td><?php echo $v;?></td>
</tr>
<?php endforeach;?>
</table>


Another very usefull situation is while using if
if($a==$b){
//lot of codes here
} else {
// more code..
}


alternative:
<?php if($a==$b):?>
// code
<?php endif;?>


// or


<?php if($a==$b):?>
// codes
<?php else: ?>
// codes
<?php endif;?>



There's another if alternative that's REALLY
usefull:

$a = $b==$c ? true : false ;


These are simple but very important situation. If your code is clean everything is easier, and that's what we want, right?

Give it a try! It's really usefull to take advantage of those alternative syntaxes:

for: endfor;
foreach: endforeach;
while: endwhile;
switch: endswitch;
if: endif;

No comments:

Post a Comment