Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Whether its OK, is partly opinionated and partly down to your specific situation. When i first started learning PHP many moons ago now, buffering the entire output into memory for post output processing seemed quite common, but it has the major pitfall of that it increases memory consumption. </p> <p>For a small site this isn't going to be a big deal. Your not going to hit any script memory limits (its only text after all, you'd need a shed load of output to do that), and your probably not going to have that much traffic that freeing memory and memory per request are on your metrics (in fact, you probably won't even be monitoring any metrics if you're asking this question).</p> <p>The second issue is particular to your example. What your asking PHP to do, is scan the entire text held in that variable, looking for a particular string and then replace it. This isn't particularly an expensive operation unless your <code>str_replace</code>ing a good few things, but it's not the best, ideal nor an advisable way to achieve what you want.</p> <p>Think of PHP much like a <code>str_replace</code> function. It runs/executes BEFORE your HTML is outputted. So you can use PHP to place text inside HTML, before it gets sent to the user. Since we can do this, we can put PHP code inside our HTML, which will be run and in most cases print some HTML/text.</p> <p>So let's look at your example...</p> <p>Instead of inserting "tags" into your HTML to be replaced, we can put some PHP directly into the HTML</p> <pre><code>&lt;?php $title = "My first website"; ?&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; Some stuff here... &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And it doesn't just have to be one simple echo inside the HTML (don't forget the tags so the PHP interpreter knows that it needs to do something though). We could do something a little more fancy in our title tag...</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt; &lt;?php if($_SERVER['PHP_SELF'] == 'index.php') { echo 'My Website :: Homepage'; } else { $pagename = array_shift(explode('.', $_SERVER['PHP_SELF'])); echo 'My Website :: '.$pagename; } ?&gt; &lt;/title&gt; &lt;/head&gt; &lt;body&gt; Some stuff here... &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Here's a different way you could do using functions. Lets create a file called <code>template_functions.php</code>. </p> <pre><code>function header($title = 'Some default title', $meta_kw = '', $meta_descr = '') { ?&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt; &lt;meta name="keywords" content="&lt;?php echo $meta_kw; ?&gt;" /&gt; &lt;meta name="description" content="&lt;?php echo $meta_descr; ?&gt;" /&gt; &lt;/head&gt; &lt;body&gt; &lt;?php } function footer() { ?&gt; &lt;div id="copyright"&gt;Copyright MyWebsite &lt;?php echo date("Y"); ?&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; &lt;?php } </code></pre> <p>Now we have a bit of reusable code, so if we want to change the main header (i.e. add facebook javascript to every page), we can do it without editing every single script. So now we can create an <code>index.php</code> like this:</p> <pre><code>&lt;?php require_once("template_functions.php"); header("My Homepage", "Some keywords", "Some description"); ?&gt; Enter site content here &lt;?php footer(); ?&gt; </code></pre> <p>and a <code>contact.php</code> page like this</p> <pre><code>&lt;?php require_once("template_functions.php"); header("Contact US", "Some keywords", "Some description"); ?&gt; Enter Your contact info, social network info, a map or a contact form here if you want! &lt;?php footer(); ?&gt; </code></pre> <p>This should hopefully show you have to start mixing PHP with HTML and even have the added bonus of functions which allow you to re-use bits of code. Templating can get far more complex that this, but i don't want to blow your mind. Start small...</p> <p>To finalise my very long answer, let's address the question put differently. Is it wrong to ever use tags in HTML and process them? The answer to this is NO. You must decide what works for you and your situation. Sometimes it's unavoidable, sometimes it's just a better way of doing things. However you usually find when you do require this sort of thing, it's on a much smaller scale of text (i.e bbcode parsers for a user comment, or replacing tags in email content that is saved in a txt file somewhere).</p> <p>Hopefully this answer helps you out! Any questions we're here to help</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload