How to submit form in a PHP file in WordPress?

I want to submit it to the file sub.php(which is in same directory)

Don’t do this. You should not have standalone PHP files that get queried via forms or AJAX etc, WordPress handles all the requests. By having a standalone file, you open a can of worms of security issues, and other problems ( you now have to bootstrap WP in your sub.php to use WP functions, which you shouldn’t need to do if you did things correctly to begin with ).

Also keep in mind that you could easily just do include( 'sub.php' ) in the form handling code, you don’t have to completely rewrite the entire thing, just make sure it isn’t possible to call it directly.

So:

  • Use the REST API if you want to talk to your site with javascript, the register_endpoint function is all you need for this
  • Use the same page you’re on, and an empty action to handle forms. This handler can be in the same template before any output happens, it could be in functions.php or a plugin if it’s redirecting

For example, lets say I have this form in my themes template, and if I enter a secret word I get a surprise here I can enter a word:

<form method="post" action="">
    What's the Secret Word? <input type="text" name="toms_secret_word" />
    <input type="submit" value="Submit"/>
</form>

I now have a form that submits to the same page, with a value I can check for, e.g.:

if ( isset( $_POST['toms_secret_word'] ) ) {
    if ( $_POST['toms_secret_word'] === 'open sesame' ) {
        echo "Correct!";
    } else {
        echo "Incorrect! Try again"
        // display the form
    }
} else {
    //... display the form
}

I’d advise moving the form into a file, that way you can do get_template_part( 'secretwordform' ); and have a secretwordform.php, and even a secretwordform-success.php and secretwordform-incorrect.php.

As a bonus, you can use hidden inputs and have multi-page forms. This way you have a hidden input saying which page is next, and hidden inputs for the items on other pages.