andershagbard
andershagbard

Reputation: 1137

Set value to false if value if == false

How do I set a value false if the value I set is == false.

I know can I do it like this:

$string = ('something that returns true' ? 'something that returns true' : false);

But I want something smarter.

I'm looking for something like this.

$string = ('something that returns true') ? false;

Examples I want to be false:

$string = array() ? false;
$string = '' ? false;

tl;dr: If the string I'm settings is not == true, set to false.

Upvotes: 0

Views: 93

Answers (2)

jeroen
jeroen

Reputation: 91742

Maybe I am missing something, but assuming that you want a boolean value, you just need:

$string = (bool) 'something that returns true';

This will cast your array, string, number or whatever to a boolean value.

Upvotes: 0

SeanCannon
SeanCannon

Reputation: 77996

If you're using 5.3+

$string = ('something that returns true') ?: false;

Upvotes: 4

Related Questions