It is common to see both forms used in spoken speech, which only adds to the doubt. However, great care must be taken in the choices that are made, as the error can become perpetuated, especially in writing. Both forms, although very similar, should be used in different ways.
Let’s look at the following example:
Option A: (EN) “-I have to leave before 8 pm.” / (PT) “– Tenho de sair antes das 20h.”
Option B: (EN) “-I have to leave before 7 pm.” / (PT) “– Tenho que sair antes das 19h.”
The use of the expression “ Tenho de ” or “ Tenho que ” is determined either by the context, by the grammatical framework of these expressions, or also by the intention with which the sentence is said.
If there was an obligation to do something, then we should say/write “I have to do A”, or “Tenho de fazer A”. But if we mean something that can be done, then in that case we should say “I have to do B”, or “Tenho que fazer B”.
Ter que It is an expression that should be used when we talk about something we want to do. We intend to do it, although it is not obligatory to do it. For example, it should be used when the verb goes in the sense of possessing something or holding something. Eg, “Since I’m home from school now, “Tenho que” to do my homework.”
Thus, Ter de it is an expression that conveys the desire for something, and also expresses the duty to do something. It is an expression that conveys the obligation to do something (comply with a certain law, pay a certain fine…). It is an expression that conveys the need to do something. It is an expression that corresponds to the desire to perform (or need, or obligation, or duty) the act expressed by the verb that follows the expression, thus the main verb.