Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-11-13 15:16:06

trenc
Plugin Author
From: ⛵️, currently Göteborg, SE
Registered: 2008-02-27
Posts: 574
Website GitHub

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;
	}
}

Digital nomad, sailing the world on a sailboat: 32fthome.com

Offline

#2 2014-11-13 16:45:01

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,454
Website GitHub

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

#3 2014-11-13 17:22:04

trenc
Plugin Author
From: ⛵️, currently Göteborg, SE
Registered: 2008-02-27
Posts: 574
Website GitHub

Re: Nested ternary operators

Naa … I doubt it Stef. It’s in if_custom_field tag lib. And seems old, very old. There are even code statements without brackets, brrb.

But you’re welcome to use it, if it’s correct.


Digital nomad, sailing the world on a sailboat: 32fthome.com

Offline

#4 2014-11-15 09:01:51

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#5 2014-11-15 11:18:17

etc
Developer
Registered: 2010-11-11
Posts: 5,195
Website GitHub

Re: Nested ternary operators

ruud wrote #285784:

It can be written even more beautiful ;)

Yeah, great, the ugliest part was

(strpos($cf_contents, $term) !== false) ? true : false

Offline

Board footer

Powered by FluxBB