开发者

Help me out with my sloppy HTML/PHP

开发者 https://www.devze.com 2023-03-16 15:19 出处:网络
This might sound like a silly question but how would I make this code seem...neater? echo \"<h3><font face=\'helvetica\'><font size=\'4\'><b><font color=\'B80000\'>$titl

This might sound like a silly question but how would I make this code seem...neater?

    echo "<h3><font face='helvetica'><font size='4'><b><font color='B80000'>$title</font></font></font></b> <font color='A0A0A0'>$categor开发者_开发百科y &nbsp;</font><font color='A0A0A0'><a href='profile.php?id=$userid'>$user</a></font>
    <font face='helvetica'><font size='3'><br>&nbsp;$desc</font></font><br>

       <h3><font color='101010'> &nbsp;$city,$state&nbsp;$zip&nbsp;<font color='A0A0A0'>$date</font>  </font></h3>";
?>

There's nothing wrong with the code but it looks so sloppy - I was wondering if someone could help me out with making it look neat and tidy


  1. Don't use <font> - move those rules to CSS, where you can style the <h3> element. Also! Not all computers have Helvetica, so you may want to change that to "Helvetica", Arial; so the design stays somewhat inline with your intent.
  2. Stay away from using <h3> twice - put the second h3 into a <p> or something similar, and use CSS to style that.
  3. &nbsp;$city,$state&nbsp;$zip&nbsp; is dirty - concatenate the string with spaces in PHP, then echo the string.
  4. Instead of using echo to output the PHP, just use echo inside of the HTML for the certain elements - this will make it much more readable.
  5. Use htmlspecialchars() on every piece of data that comes out of a database before you echo it to the page. This prevents invalid HTML and XSS vulnerabilities.

That should tighten it up!


Learn about MVC for PHP.

Learn about frameworks for php. Start using one.

Or start by separating CSS from other data. Use PHP to output dynamic data, use HTML/CSS for static data. Avoid mixing the two.

Links from google


move all <font ....> codes to style="" attribute

also consider moving html out of php echo, i.e.

<h3>
<a href="<?php echo ... ?>"><?php echo ... ?></a>
</h3>


I've put it into heredoc for you, but most of the changes you'll need to do yourself.

    // use heredoc's: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
    $out = <<HER 
    <h3><!-- you should use CSS to do this. You should get rid of font tags -->
        <font face='helvetica'> <!-- you don't need three either, you can put them all in one -->
            <font size='4'>
                <b>
                    <font color='B80000'>
                        $title
                    </font>
                </font>
            </font>
        </b><!-- move this so that it matches above -->

        <font color='A0A0A0'>
            $category &nbsp;
        </font>
        <font color='A0A0A0'>
            <a href='profile.php?id=$userid'>
                $user
            </a>
        </font>
        <font face='helvetica'>
            <font size='3'>
                <br> <!-- make this self closing -->
                &nbsp;$desc
            </font>
        </font>
        <br>
        <h3><!-- nested??!? You're already in H3-->
            <font color='101010'>
                &nbsp;$city,$state&nbsp;$zip&nbsp;
                <font color='A0A0A0'>
                    $date
                </font>  
            </font>
        </h3>
HER;

    ?>


The very first thing you can do is close the PHP tag, write out your HTML while leaving spaces and newlines and indenting as you open tags, and re-open PHP when you need it:

?>
<h3>
    <font face='helvetica'>
        <font size='4'>
            <b>
               <font color='B80000'><?php echo $title; ?></font>
        (...)
            </b>
        </font>
    </font>
</h3>
<?php 

Also look into the heredoc syntax.

On a non-syntactical sidenote you would be better served in the long run by using CSS instead of all those <font> tags:

<style type="text/css">

.myTitle {
    font-face: helvetica;
    font-size: large;
    color: #B80000;
}

</style>

echo "<h3 class='myTitle'>$title</h3>" ;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号