Q:

Is it okay to set title and meta description in php like this?

I'm setting page title and meta description dynamically on the pages of my site http://jobfou.com, since http is stateless, like this.
<html>
<?php
ob_start();
?>
<title> <!--TITLE--> </title>
<body>
Some HTML Page Content From Database Goes Here.

<?php
//The PHP Code Here Gets executed on each page loads, dynamically.
$wholePageContents = ob_get_contents();
$post_title = "Page 1";
ob_end_clean();
echo str_replace("<!--TITLE-->", $post_title, $wholePageContents);
?>

</body>
<html>

Is it okay to set page title and meta description like this, calling ob_start(); on each page load, what are the pros and cons, or should i just go with using jquery. Which is better, and doesn't consume memory.

A:

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. 
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).
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 str_replaceing a good few things, but it's not the best, ideal nor an advisable way to achieve what you want.
Think of PHP much like a str_replace 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.
So let's look at your example...
Instead of inserting "tags" into your HTML to be replaced, we can put some PHP directly into the HTML
<?php
$title = "My first website";
?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
Some stuff here...
</body>
</html>

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...
<html>
<head>
<title>

<?php
if($_SERVER['PHP_SELF'] == 'index.php') {
   echo 'My Website :: Homepage';
} else {
   $pagename = array_shift(explode('.', $_SERVER['PHP_SELF']));
   echo 'My Website :: '.$pagename;
}
?>
</title>
</head>
<body>
Some stuff here...
</body>
</html>

Here's a different way you could do using functions. Lets create a file called template_functions.php. 
function header($title = 'Some default title', $meta_kw = '', $meta_descr = '')
{
?>
<html>
<head>
<title><?php echo $title; ?></title>
<meta name="keywords" content="<?php echo $meta_kw; ?>" />
<meta name="description" content="<?php echo $meta_descr; ?>" />
</head>
<body>
<?php
}

function footer() {
?>
<div id="copyright">Copyright MyWebsite <?php echo date("Y"); ?></div>
</body>
</html>
<?php
}

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 index.php like this:
<?php
require_once("template_functions.php");

header("My Homepage", "Some keywords", "Some description");
?>
Enter site content here
<?php
footer();
?>

and a contact.php page like this
<?php
require_once("template_functions.php");

header("Contact US", "Some keywords", "Some description");
?>
Enter Your contact info, social network info, a map or a contact form here if you want!
<?php
footer();
?>

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...
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).
Hopefully this answer helps you out! Any questions we're here to help

