Paul
Paul

Reputation: 654

php double if statement

I'm new to learning how to program. And I'm wondering what the best way is to handle the problem where you got a double if statement, with both having the same Else result.

Take for instance the following double if statement.

if (isset($x)) {
    $y = doSomething($x);
    if ($y == "something") {
        do result A;
    }
    else {
        do result B;
    }
}
else {
    do result B;
}

It doesn't seem like a smart idea to write result B multiple times. Now what are the different ways to prevent having to write result B multiple times?

One can try making 1 combined if statement, but this doesn't seem always possible (for instance when checking with isset() if a variable exists).

if (isset($x) && $y) { etc. }

What other options are there?

PS. I'm also open for other suggestions to help improve my code, like how to write if(isset($x)) in a nicer way.

Upvotes: 1

Views: 1229

Answers (4)

Paul
Paul

Reputation: 654

Thank you for your help all.

I found the following to be the easiest and most clear way to solve this. Now result B is only written once instead of multiple times. Especially if result B gets long, this keeps it easy to read.

$failure = false;

if (isset($x)) {
    $y = doSomething($x);
    if ($y == "something") {
        do result A;
    } else {
        $failure = true;
    }
} else {
   $failure = true;
}

if ($failure == true) {
    do result B;
}

Upvotes: 0

user1544337
user1544337

Reputation:

if (isset($x) && doSomething($x) == 'something') {
    // do result A;
}
else {
    // do result B;
}

Important: doSomething($x) will only be calculated if isset($x) evaluates to true. Otherwise, checking the condition is aborted directly and the else-branch will be executed. So you don't have to worry about doSomething($x) giving any side effects if $x isn't set.

I'm not sure if this is documented behaviour, but it can be checked with this:

<?php
function checkSomething() {
    echo "Look, I'm checking something!\n";
    return true;
}

if (1 == 0 && checkSomething()) {
    echo "if\n";
} else {
    echo "else\n";
}

The output:

else

If checkSomething() would've been checked, it would have outputted if and also the echo in checkSomething().

Upvotes: 2

developerwjk
developerwjk

Reputation: 8659

You probably want to use the ternary operator to set $y.

$y = (isset($x)) ? doSomething($x) : "some_other_value";
if ($y == "something") 
{
  do result A;
}
else 
{
   do result B;
}

If $x isset then it puts the value returned by doSomething($x) in $y, otherwise some other value you specify. Then you do your if-statement once on the value of $y.

Upvotes: 0

Andrew Smith
Andrew Smith

Reputation: 210

if (isset($x) && doSomething($x) == "something") {
    do result A;
}
else {
    do result B;
}

or

$successful = false;
if (isset($x)) {
    $y = doSomething($x);
    if ($y == "something") {
        $successful = true;
    }
}

if ($successful) {
    do result A;
} else {
    do result B;
}

Upvotes: 4

Related Questions