3

I am studying PHP from a book. The author uses echo to output HTML. At first I thought that this was how it supposed to be done, but then I picked up a more "advanced" book. The author of the second book inserted PHP code between HTML rather then echo the whole thing. How is it done in large web development companies that work on large projects? Can you use either, or is one more accepted than the other?

Take this code for example:

<?php
//pagination
if ($pages > 1) {
    //determine the current page
    $current_page = ($start / $display) + 1;

    //print out Previous Page button
    if ($current_page != 1) {
?>
        <div class="pages"><strong><a href="view_users.php?s=<?php echo ($start - $display); ?>&amp;p=<?php echo $pages; ?>">&nbsp;&lt;&nbsp;</a></strong></div>
<?php
    }

    //print the page numbers
    for ($i = 1; $i <= $pages; $i++) {
        if ($i == $current_page) {
?>
         <div class="pages active"><span>&nbsp;<?php echo $i; ?>&nbsp;</span></div>
<?php
    }
    else {
?>
        <div class="pages"><strong><a href="view_users.php?s=<?php echo ($display * ($i - 1)); ?>&amp;p=<?php echo $pages; ?>">&nbsp;<?php echo $i; ?>&nbsp;</a></strong></div>
<?php
        }//end of $i = $current_page conditional
    }//end of FOR loop

    if ($current_page < $pages) {
?>
        <div class="pages"><strong><a href="view_users.php?s=<?php echo ($start + $display); ?>&amp;p=<?php echo $pages; ?>">&nbsp;&gt;&nbsp;</a></strong></div>
<?php
    }
}//end of pagination

include('includes/footer.php'); 
?>

Is that the correct way of doing it, or should I use something like this:

echo '<a href="view_users.php?s=' . ($display * ($i - 1)) . 
'&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> ';

The reason why I'm asking is because I find it difficult to properly align the HTML when using the second technique, and I don't want to develop any bad habits early on.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
n1te
  • 945
  • 3
  • 11
  • 20
  • Another consideration is PHP's alternative syntax. This http://stackoverflow.com/questions/564130/difference-between-if-and-if-endif goes over various code formatting styles. – micahwittman Jun 27 '12 at 06:05
  • A good little tutorial that covers separation of concerns (just using the inherent templating nature of PHP): http://phpstarter.net/2009/03/separating-the-application-presentation-and-alternative-syntax/ – micahwittman Jun 27 '12 at 06:14
  • @micahwittman thanks, the link that you gave clears things up. – n1te Jun 27 '12 at 06:20

6 Answers6

4

The first method makes your code more readable. The second method is useful when you need to print some little amount of html code. Also you can use short open tags to insert your php code between html tags. For example if you need to print language parameter of URL you can do like this:

<a href="index.php?lang=<?=$lang?>" >A Link</a>

This is equal to this:

<a href="index.php?lang=<?php echo $lang; ?>" >A Link</a>
Rafael Sedrakyan
  • 2,541
  • 10
  • 34
  • 42
1

I'd recommend using a good framework in general, and a templating engine for outputting pages. This way, your data models, your application logic, and your HTML are all kept separate.

avramov
  • 2,119
  • 2
  • 18
  • 41
  • You'd still end up echoing PHP variables out at some point whether you use a framework or not. – Gabriel Baker Jun 27 '12 at 05:52
  • But not if using a *templating engine*? I might be wrong though - I left the PHP world for good after one project too many, and have now happily migrated to Django; do PHP frameworks not have such amenities as templating? – avramov Jun 27 '12 at 06:00
  • At some point I want to start learning frameworks and MVC but first I want to learn the basics, that's why I asked the question. – n1te Jun 27 '12 at 06:01
  • PHP is a templating engine - there is no point in putting a template engine on top of a template engine. – Repox Jun 27 '12 at 06:07
  • @Repox - that's an interesting point of view which I hadn't considered before. Still, maintainability is greater IMO if using the intentionally limited feature set of a simplified template language. This way, you can have better separation of back-end and front-end code, which is related to OP's question: it's something large companies would want to do on large projects so different people could easily work on different parts of a project without messing up each other's code. – avramov Jun 27 '12 at 08:10
  • @n1te - "at some point" is too late for learning a good programming practice that's going to make your life a lot easier in the long run. – avramov Jun 27 '12 at 08:11
1

This is a somewhat subjective question, but:

echo '<a href="view_users.php?s=' . ($display * ($i - 1)) . 
'&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> ';

This is not very good, because it quickly becomes hard to read. It's the worst option to use IMO.

Here an evolution on how this can be improved:

<a
 href="view_users.php?s=<?php echo $display * ($i - 1); ?>&p=<?php echo $pages; ?>&sort=<?php echo $sort; ?>">
    <?php echo $i; ?>
</a>

<?php
    printf('<a href="view_users.php?s=%i&p=%i&sort=%s">%i</a>',
           $display * ($i - 1), $pages, $sort, $i);
?>

<?php
    $q = http_build_query(array(
        's'    => $display * ($i - 1),
        'p'    => $pages,
        'sort' => $sort
    ));
?>
<a href="view_users.php?<?php echo $q; ?>"><?php echo $i; ?></a>

<?php
    printf('<a href="view_users.php?%s">%i</a>',
           http_build_query(array(
               's'    => $display * ($i - 1),
               'p'    => $pages,
               'sort' => $sort
           )),
           $i);
?>
deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thanks for the clarification. Could you please refer me to some reading material for the method you used to 'evolve' the code. – n1te Jun 27 '12 at 06:10
  • `printf` to highligth code **iMNHO** is a big no-no Best would be `$href = http_build_query(...` and `?>= $label ?>` – percebus Oct 28 '14 at 17:03
0

Paul, what you're looking at in that code is a combination of PHP, CSS and HTML. echo just means print on page and it is a PHP function which is why you see it with the tags.

<?php ?>

Does this answer your question?

Also PHP and HTML and CSS will not parse any whitespace so indenting is done to enhance readability. I suggest you comment your code and format it however it is easier for you to understand. From a professional standpoint check out the W3C validator for proper code formatting.

MrPizzaFace
  • 7,807
  • 15
  • 79
  • 123
0

Both are acceptable really, using echo for short statements and completely breaking out of php for long bits of html. Indentation of your HTML doesn't matter that much, it just looks pretty when someone checks out your source.

Gabriel Baker
  • 1,209
  • 11
  • 21
0

wrapping html generation in functions and building output strings can be a good practice. That way you can have complex logic expressed in a readable way:

<?php

function get_pagination_html($pages, $start, $display) {
    $html = "";

    if ($pages <= 1) return $html;

    $current_page = ($start / $display) + 1;

    if ($current_page != 1) {
        $previous = $start - $display;
        $html .= "<div class='pages'><strong><a href='view_users.php?s={$previous}&amp;p={$pages}'>&nbsp;&lt;&nbsp;</a></strong></div>";
    }

    for ($page_number = 1; $page_number <= $pages; $page_number++) {
        if ($page_number == $current_page) {
            $html .= "<div class='pages active'><span>&nbsp;{$page_number}&nbsp;</span></div>";
        } else {
            $page_start = $display * ($page_number - 1);
            $html .= "<div class='pages'><strong><a href='iew_users.php?s={$page_start}>&amp;p={$pages}'>&nbsp;{$page_number}&nbsp;</a></strong></div>";
        }
    }

    if ($current_page < $pages) {
        $next = $start + $display;
        $html .= "<div class='pages'><strong><a href='view_users.php?s={$next}&amp;p={$pages}'>&nbsp;&gt;&nbsp;</a></strong></div>";
    }

    return $html;
}


echo get_pagination_html($pages, $start, $display);

Notice how much easier it reads. You also don't need any comments because variable names are self explanatory

Konstantin
  • 3,294
  • 21
  • 23