What is the best practice for prefilling text fields in PHP forms?

When prefilling text fields in PHP forms, it's best practice to check if the form has been submitted and if the field data is available. If the data is available, populate the text fields with the submitted values. This ensures that users do not lose their input if there are validation errors or if they need to correct other form fields.

<?php
// Check if form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Prefill text fields with submitted values
    $name = isset($_POST['name']) ? $_POST['name'] : '';
    $email = isset($_POST['email']) ? $_POST['email'] : '';
} else {
    // Initialize text fields if form has not been submitted
    $name = '';
    $email = '';
}
?>

<form method="post" action="">
    <input type="text" name="name" value="<?php echo $name; ?>" placeholder="Name">
    <input type="email" name="email" value="<?php echo $email; ?>" placeholder="Email">
    <button type="submit">Submit</button>
</form>