Why is it not entering on the right conditional side? - a simple logic issue perhaps?

Hello all,

I have the following:


infosPreenchidosInputsVazios = ( ( !empty($hostNameInfo) || !empty($hostAddressInfo) ) && ( empty($hostNameInput) && empty($hostAddressInput) ) );

The dump returns:


var_dump($hostNameInput); //dump: string(0) "" 
var_dump($hostAddressInput); //dump: string(0) "" 
var_dump($hostNameInfo); //dump: object(SimpleXMLElement)#22 (1) { [0]=>  string(14) "ns1.ada.org." }
var_dump($hostAddressInfo); //dump: string(11) "82.2.34.14"

Then I have:


elseif($infosPreenchidosInputsVazios)
{
   echo ("PLEASE, SHOW YOURSELF"); 
}

But this echo doesn’t appear.
Could this be because somehow, the previous if before this elseif evaluates to true, hence this one, despite been logical correct never executes?

Thanks in advance,
Márcio

So there is a condition test missing from the first if. The easiest fix is to reverse the order and make the elseif into the if and have the if as the elseif

Thanks felgal,

I end up changing a little here and there, due to the fact that I wanted to have, on the first if, something that will normally arrive. And on the later, something that doesn’t arrive so often. :slight_smile: (proud newbie). :slight_smile:

Regards,
Márcio

Thanks a lot.

Yup, :slight_smile: he is running the first if.

Need to better think the logic behind. It should never run the first if on those circumstances. It seems that I do have a too wide formula that works for both, and I need to logically narrow it.

Thanks again for the detailed explanation. :smiley:

Márcio

Yup, that’s exactly what’s happening.

Try this:


if (true)
{
   echo "This text will appear";
}
elseif (true)
{
   echo "This text will <em>never</em> appear.";
}

elseif statements are only executed if all previous if statments and/or other elseif statements evaluated to false.

Put differently,


if ($something)
{
   doSomething();
}
elseif ($somethingElse)
{
   doSomethingElse();
}

does exactly the same as:


if ($something)
{
   doSomething();
}
else
{
   if ($somethingElse)
   {
       doSomethingElse();
   }
}