Praise be to Allah, and may peace and blessings be upon the Messenger of Allah. To proceed:
The word “if” is not prohibited in all circumstances. Rather, it is used in two ways:
First: It may be said as an expression of sorrow, regret, and distress over something that has already happened and been decreed by Allah, such as when a person says after a calamity occurs: “If only I had done such-and-such, this would not have happened.” This is what has been prohibited, because it opens the door to grief and distress and does not benefit the person.
The Prophet ﷺ said: “If something befalls you, do not say: ‘If only I had done such-and-such, then such-and-such would have happened,’ but say: ‘Allah has decreed, and whatever He wills, He does; for ‘if’ opens the work of Satan.” Narrated by Muslim.
Therefore, when a calamity occurs, the Muslim must know that what befell him could never have missed him, and what missed him could never have befallen him. He should therefore be content with Allah’s decree and submit to it.
Second: The word “if” may be used to explain beneficial knowledge, or to express love of goodness and a desire for it. This is permissible, as in the statement of Allah تعالى: ﴿Had there been gods therein besides Allah, they both would have been ruined﴾ [Al-Anbiya: 22].
Another example is the Prophet’s ﷺ saying: “May Allah have mercy upon Moses. I wish he had been patient so that Allah could have told us more about their story.” Here, the intended meaning is not regret over a past decree. Rather, it is an expression of the Prophet’s ﷺ love of patience, because of the benefit that would have resulted from it, namely learning the remainder of the story and the lessons contained within it.
Therefore, the prohibition of “if” is not a prohibition of the word in every circumstance. Rather, it concerns using it to express regret, objection, and distress over something that has already happened and passed. As for using it to explain something beneficial, or to express love of goodness and a desire for it, then it is permissible.
And Allah تعالى knows best.