8

Instructor: "and now here we can write this while loop in ph- oh wait. Let me just show you a cool trick. I am gonna close this php tag with ?> , open an html tag , then again open this php tag , add this line, close it, then again close html tag, then again open php, and then close it. pretty crazy huh? You see, now we can have our cool bootstrap displaying this output beautifully"

Me : FUCK YOU MAN :|
for reference, this was the final output :
============================
<?php
while($row = mysqli_fetch_assoc($result_successful)){
?>
<pre> <?php print_r($row); ?> </pre>
<?php
}
============================

I am a newbie in web dev who comes from a very nice java/kotlin world. is this a common site in web dev, mixing of all the html/php/some 1000 more laguages and frameworks to make 1 working product? coz it sucks.

Comments
  • 6
    *sees udemy tag*
    *laughs violently*
  • 3
    It's also a language that grew from a temporary need. But yeah, PHP sucks.

    And no, that's just PHP.
  • 5
    OH MY GOD, NOW HE'S CREATING A PHP FILE footer.php WITH ONLY 3 HTML TAGS AND NOTHING ELSE, AND THAT TOO:

    </div>
    </body>
    </html>

    IN THE NAME OF MODULARIZATION!!!

    i am gonna puke🤮 🤮 🤮
  • 5
    @StopWastingTime Sounds like wasted money to me, lmao
  • 2
    PSR-1 disallows this (2.3). It works because PHP is a templating language at its roots, but it's discouraged. The only practical reason to learn about this is to not be surprised later when you do this by accident.
  • 2
    @ScriptCoded no that is rape... Just that you don't like how she dresses doesn't make it right!

    FFS at least use alternative syntax not that it's needed here; one can obviously go without the tags and just print the damn thing.
  • 0
    Okay, if I'm not supposed to do this then how exactly do I create templates in PHP? (Ignoring of course the option to unnecessarily print the whole thing which leads to the same result but with more commands and no chance for syntax highlighting)
  • 1
    @Lor-inc You use a proper templating engine like Twig or Blade, which will enforce view-logic separation.
  • 0
    @Lor-inc it is best to use a true template parser like twig but if that is overkill it can be done like this
    <?php

    //Includes and boilerplate here
    $result = $db->query('select stuff');

    ?>
    <!DOCTYPE html>
    <html>
    <body>
    <ul>
    <?php while ($row = $result->fetch(PDO::FETCH_OBJ)):?>
    <li>
    <label>name:</label>
    <span><?php echo $row->name;?></span>
    </li>
    <?php endwhile; ?>
    </ul>
    </body>
    </html>

    Excuse the poor formatting. It's hard on a phone. Usually it is used when there is a lot of html and very little PHP inside the flow control statements
  • 1
    @hjk101 If there's more than a little PHP in a template then syntax isn't your greatest problem.
  • 2
    Thanks
Add Comment