How can checkboxes be preselected based on user input in PHP forms?

To preselect checkboxes based on user input in PHP forms, you can use the 'checked' attribute in the HTML input tag. You can check if the checkbox value matches the user input and add the 'checked' attribute if it does.

<input type="checkbox" name="option1" value="1" <?php if(isset($_POST['option1']) && $_POST['option1'] == '1') echo 'checked'; ?>>
<input type="checkbox" name="option2" value="2" <?php if(isset($_POST['option2']) && $_POST['option2'] == '2') echo 'checked'; ?>>
<input type="checkbox" name="option3" value="3" <?php if(isset($_POST['option3']) && $_POST['option3'] == '3') echo 'checked'; ?>>