Kaz
Kaz

Reputation: 1278

Which one is better or must be in PHP scripts that can be applied for real projects?

As I am learning PHP, I want to know if I ever wanted to put HTML tags in the middle of a PHP script, I want to know which one is better or must be, out of putting them in PHP echo or breaking the PHP script where the HTML has to be started and then continuing the PHP script using tags php starting and closing tags. Below are two examples where you can get a clear idea.

page name: home.php example: 1
<!DOCTYPE html>
 <html>
  <head></head>
   <body>
    <?php
     a=0;
    while (a<=10){
   ?>
   <input type = "text" max-length="20">
   <?php
   }
   ?>
   </body>
 </html>

 page name: home.php example: 2
<!DOCTYPE html>
 <html>
  <head></head>
   <body>
    <?php
    a=0;
    while (a<=10){
   echo "<input type = 'text' max-length='20'>";
   }
   ?>
   </body>
 </html>

Upvotes: 0

Views: 51

Answers (2)

Ken Sawyerr
Ken Sawyerr

Reputation: 236

For learning purposes I will suggest you use <?php ?> and <?= ?> to break out of HTML. Echo’ing HTML may confuse you while learning (as someone earlier pointed out code highlighting). In your journey through learning, consider learning about MVC and how you can apply in simple ways to fit your needs. It helps you separate presentation from logic.

Upvotes: 1

Anthony L
Anthony L

Reputation: 2169

This is really just a matter of preference, both are acceptable. Typically if you have quite a bit of HTML DOM to insert, you'd break php with ?> and then reopen when necessary. If you have just a single line of HTML that needs to be inserted, a simple echo works perfectly.

Upvotes: 0

Related Questions