0

i would to know what is good practice for writing code to put all HTML code inside PHP function and in my front index.php file just call function to show code.

class.php:

  public function test() {

  $sql='select id,title from test ';
  $nem=$this->db->prepare($sql);
  $nem->execute();
  $nem->bind_result($id,$title);

  echo '<ul class="centreList">';
  while($nem->fetch()) 
      {       
        echo '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
      }
  echo '</ul>'; 

 }

index.php:

<?php $connection->test(); ?>

This work fine, but I would like to know is this proper way or is not a good practice to use html code inside PHP functions?

5
  • 1
    It's fine, but you shouldn't echo it directly. Return the string and echo the function instead. Commented Jul 22, 2017 at 14:24
  • Hi,can you be just litle specific ?:) Thanks in advance ! Commented Jul 22, 2017 at 14:26
  • 1
    Not "good practice", many will tell you to return instead of echo. But, honestly, it works, right ? :) rename it "printTest" and that's it ;) Commented Jul 22, 2017 at 14:26
  • It's a matter of having control over what's being output where, @PierreGranger. Yeah, it works, and its fine, nobody will arrest you fpr ot (there's truth to the expression "if its stupid, but works - it ain't stupid"), but to have proper control over what you're doing, it should be returned and not printed directly ;-) Commented Jul 22, 2017 at 14:33
  • Yeah of course you're right, but sometimes it's good to be bad ;) and if you rewrite it function printTest() then you know exactly what it does... like print_r ;) Commented Jul 22, 2017 at 14:35

2 Answers 2

1

It's ok to build HTML within PHP, but I would not echo to the screen directly from within the function. Instead, return the built HTML string.

 $html = '<ul class="centreList">';
  while($nem->fetch()) 
      {       
        $html .= '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
      }
  $html .='</ul>'; 

  return $html

The function should not be responsible for pushing content to the browser because it really limits what you can do with your code. What if you wanted to further process the HTML? What if you run into a condition later in the code and decided to abort? What if you wanted to set some response headers later? Some content would already be gone so none of these things would be possible without clever workarounds.

In general you want to separate your responsibilities: I would even break things down further:

  • one piece of code is in charge of retrieving info from the DB and returning
  • Another piece is in charge of building the HTML string
  • A third piece is in charge of displaying the HTML (probably your index.php)

New index.php

<?= $connection->test(); ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Removed my answer because this one is better. The only thing I would disagree with here is that the html should not be built within the function either. The function should only return data and then in the html-part there should be PHP to render the html. Also, you're wrong at the end there. If you use return, you should echo the function, not just call it.
@Babydead The short tag <?= is an alias for <?php echo, so that's what I'm doing. See this
0

Do not use echo to print the html directly, wrap the html within while loop surrounded by php tags

public function test() {

  $sql='select id,title from test ';
  $nem=$this->db->prepare($sql);
  $nem->execute();
  $nem->bind_result($id,$title);

  return $nem;
}

<ul class="centreList">
    <?php  $res = test()->fetch(); 
           while( $res->fetch() ) { ?>
        <li> <?php echo $id ?> <a href="<?php $title ?>"> Download </a></li>; 
    <?php } ?>
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.