The latex format defines \TextorMath
which is a very thin wrapper around \ifmmode
You don't say what defined \textormath
but it is presumably similar.
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname protected\endcsname\relax
\DeclareRobustCommand\TextOrMath{%
\ifmmode \expandafter\@secondoftwo
\else \expandafter\@firstoftwo \fi}
\protected@edef\TextOrMath#1#2{\TextOrMath{#1}{#2}}
\else
\protected\expandafter\def\csname TextOrMath\space\endcsname{%
\ifmmode \expandafter\@secondoftwo
\else \expandafter\@firstoftwo \fi}
\edef\TextOrMath#1#2{%
\expandafter\noexpand\csname TextOrMath\space\endcsname
{#1}{#2}}
\fi
In general it's better to use the latex form unless you know the context and it it is safe to use the TeX primitive, notably in section headings and at the start of table cells \ifmmode
acts in a well defined way but not as you may expect and not usefully, (it is always false at the start of an array cell for example)

\documentclass{article}
\begin{document}
$
\begin{array}{c}
\ifmmode math \else text\fi \\
{}\ifmmode math \else text\fi \\
\TextOrMath{text}{math} \\
{}\TextOrMath{text}{math} \\
\end{array}
$
\end{document}
}
in your\ifmmode
example, note that it doesn't take brace arguments but\ifmmode yes \else no \fi
t1enc.def
), whereas the second may work wrongly inarray
(it should have\relax\ifmmode
).