What you will want to strive for as you progress is to as much as possible separate your HTML output from your PHP logic. If you have to keep alternating between HTML text and PHP if/else blocks, nested ifs, switches, etc., it becomes more and more difficult to read you code. That means it becomes more and more difficult to debug or modify your code. (And just imagine if someone else has to debug it!)
Thus you see people using various templating systems, separate "views" (as in a MVC framework), etc. At the very least, for something like this I recommend you set any conditionally set variables first, and then use them in your HTML output.
<?php
// DB code that populates $row, then...
// set your conditional values:
if(empty($_POST['first_name']))
{
$first_name = $row['first_name'];
}
else
{
$first_name = $_POST['first_name']
}
// or here's a short-hand way to do the same thing:
$last_name = (empty($_POST['last_name'])) ? $row['last_name'] : $_POST['last_name'];
// now do the output:
echo "
<form action='foo.php' method='post'>
<p><input type='text' name='first_name' value='$first_name' /></p>
<p><input type='text' name='last_name' value='$last_name' /></p>
<p><input type='submit' value='Submit' /></p>
</form>
";
Alternatively, you could drop out of PHP mode for the HTML output, then pop back in as needed to output the variables:
<?php
// DB code that populates $row, then...
// set your conditional values:
$first_name = (empty($_POST['first_name'] ? $row ['first_name'] : $_POST['first_name']
// or here's a short-hand way to do the same thing:
$last_name = (empty($_POST['last_name'])) ? $row['last_name'] : $_POST['last_name'];
// now do the output:
?>
<form action='foo.php' method='post'>
<p><input type='text' name='first_name' value='<?php echo $first_name; ?>' /></p>
<p><input type='text' name='last_name' value='<?php echo $last_name; ?>' /></p>
<p><input type='submit' value='Submit' /></p>
</form>