0

I am wondering which one of the following is a better approach when I call require_once(file.php):

file.php

<?php
    $name = "Three";
    echo "<h1> Header Tag : $name </h1>";
?>

OR

file.php

<?php
   $name = "Three";
?>

<h1> Header Tag : <?=$name?> </h1>

I'm curious to know if using either one of these could cause any sort of problem down the road in execution.

FreeKrishna
  • 1,833
  • 2
  • 12
  • 21
  • The result is identical. The difference in code is self-evident and ultimately a matter of style and/or applicability which you can decide for yourself. – deceze Feb 20 '17 at 14:24
  • @deceze So is it okay to use either of these methods under any circumstances? I mean is there an appropriate convention that could be followed? I'm trying to learn about better coding approaches and want to make sure that i'm on the right track. Thanks. – FreeKrishna Feb 20 '17 at 14:28
  • Generally I'd prefer the second, instead of embedding HTML in PHP strings in PHP blocks. But both have their uses… – deceze Feb 20 '17 at 14:30
  • Okay good to know. Thanks. Any material you could recommend for a newbie to learn better coding practices ? – FreeKrishna Feb 20 '17 at 14:46
  • To appreciate significant performance differences between double-quoted strings and raw HTML you'd probably need to include several million lines of code and that would lead to entirely different performance problems. The benefit of just writing clean code outweighs those considerations. – Álvaro González Feb 20 '17 at 15:19
  • 1
    The question was confusing and has been incorrectly marked as duplicate of [Difference between require, include and require_once](http://stackoverflow.com/questions/2418473/difference-between-require-include-and-require-once). It's a duplicate of [Escape HTML or Use echo?](http://stackoverflow.com/questions/505642/escape-html-to-php-or-use-echo-which-is-better) instead – rds Feb 20 '17 at 21:17
  • @rds Not entirely. I'm more curios regarding the effect of these two types when used under `require_once()`. I wasn't sure how it would get inserted in the main page. Whether the included file breaks existing php block and inserts it as a new block or something else was my doubt. And whether it had any side effects further. I know its usually better to keep HTML separate from PHP under normal circumstances, but since i'm using `require_once` wasn't sure on how it exactly inserted the code and executed. The official site doesnt have any proper documentation about same. :) – FreeKrishna Feb 21 '17 at 05:12

0 Answers0