IF in text LOW:HIGH

I would like to insert IF directly into the text to make the script as simple as possible. How will they advise? This doesn't work:

String text_err = "text-warning";
String text_ok = "text-success";
HTML += "<td class=\"text-right" + (temp == 127.0) text_err : text_ok + "\">" + String(temp) + " °C</td>";

What exactly doesn't work ?
There is no if statement in the code that you posted. What exactly are you trying to do ?

If temp == 127.00, I need to print text_err and vice versa text_ok
I would like to use IF in the text so that I don't have to deal with IF outside. I will have about 25 DS18B20 thermometers there and I want to check the color status.

IF in text:

(temp == 127.0) text_err : text_ok

Maybe something like this:

String text_err = "text-warning";
String text_ok = "text-success";
HTML += "<td class=\"text-right" + ((temp == 127.0) ? text_err : text_ok ) + "\">" + String(temp) + " °C</td>";

but search also for "c++ ternary operator"

1 Like

Personally I would keep things simple and as easy to read as possible

String tempText = "text-warning";
if (temp == 127)
{
  tempText = "text-error";
}
HTML += "<td class=\"text-right" + tempText + " °C</td>";

In practice I would probably not use a String at all

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.