tomJO
tomJO

Reputation: 371

Most efficient way to shorten an if statement

How would one shorten an if statement if code is as below ?

$a = null;
$b = "foo";

if ((empty($a) && !empty($b)) || (!empty($a) && empty($b)) {}

Upvotes: 3

Views: 92

Answers (2)

Nigel Ren
Nigel Ren

Reputation: 57121

The test seems to be checking that only 1 field contains a value, so you could check

$a = null;
$b = "foo";

if (empty($a) != empty($b)) {}

Upvotes: 5

Universus
Universus

Reputation: 406

$a = null;
$b = "foo";
if(empty($a) xor empty($b)){}

Upvotes: 8

Related Questions