Here's a rough example, you will need to edit it to fit your project obviously. Maybe you're submitting to a different script, ask for email from a different script, etc.
You can leverage the session to store data temporarily.
session_start();
echo '<form action="" method="post">
<input type="text" name="otp" />
<input type="submit" value="Submit" />
</form>';
// if the form is submitted, then check for OTP, otherwise generate it and store it in session
if (isset($_POST['otp'])) {
// check if the OTP is correct
if ($_POST['otp'] == $_SESSION['otp']) {
//do something
} else {
// do something else
}
}
else{
$otp = rand(100000, 999999); //this is not a safe way to generate an OTP, but just for examples sake
$_SESSION['otp'] = $otp;
// code that sends the OTP to the user here using mail, text or API...
// ...
}