Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Nested ternary operators
I hate it … really.
I’m writing a new plugin and I want to borrow some code from the core but not without some beautifying.
I found this damn nested ternary:
$cond = is_array($cf_contents) ? in_array($term, $cf_contents) : ((strpos($cf_contents, $term) !== false) ? true : false);
Can anybody unravel this. Am I correct with this translation:
$cond = false;
if (is_array($cf_contents))
{
if (in_array($term, $cf_contents))
{
$cond = true;
}
}
else
{
if (strpos($cf_contents, $term) !== false)
{
$cond = true;
}
}
Offline
Re: Nested ternary operators
Ughhh, that’s damn ugly. I hope I didn’t write that line in a moment of madness…
Anyway, your translation looks good to me. I might borrow it back for the core ;-)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Offline
Re: Nested ternary operators
trenc wrote #285722:
I found this damn nested ternary:
$cond = is_array($cf_contents) ? in_array($term, $cf_contents) : ((strpos($cf_contents, $term) !== false) ? true : false);
It can be written even more beautiful ;)
$cond = is_array($cf_contents) ? in_array($term, $cf_contents) : !is_bool(strpos($cf_contents, $term));
Offline
Offline
Pages: 1