Link to home
Start Free TrialLog in
Avatar of EICT
EICTFlag for United Kingdom of Great Britain and Northern Ireland

asked on

php IF statement problem

Hi,
I think I've spent too many hours on my computer and just can't see why i'm getting the wrong answer displayed.

I have the following php code:
----------------------------------------------------------------
          $Under6MonthsLACode='00KC';
          $Under6MonthsLAYears = 0;
          $Under6MonthsLAMonths = 1;
          $Under6MonthsLADays = 2;
          $ServiceAAYears = 0;
          $ServiceAAMonths = 2;
          $ServiceAADays = 3;

if (
($Under6MonthsLACode == 0)
|| (($Under6MonthsLAYears == 0) && ($Under6MonthsLAMonths == 0) && ($Under6MonthsLADays == 0))
|| (($ServiceAAYears == 0) && ($ServiceAAMonths == 0) && ($ServiceAADays == 0))
)          { $HostDurationUnknown = 1;} else {$HostDurationUnknown = 0;}
       
echo $HostDurationUnknown.'<br>';

---------------------------------------------------------

This IF statement is supposed to check to see if
1. The $Under6MonthsLACode is set to 0
or
2. All of the $Under6MonthsLA... Variables (Years, Months or Days) are set to 0
or
3. All of the $ServiceAA... Variables (Years, Months or Days) are set to 0
if one of the above is true then set $HostDurationUnknown to 1.

So why when all the above are not true is it displaying 1?

I'm sure its staring at me!!

Thanks





SOLUTION
Avatar of haloexpertsexchange
haloexpertsexchange
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Francisco Igor
Francisco Igor
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The conditional

if (($Under6MonthsLACode == 0)

is true, so it will not check the OR statements

To a conditional OOKC looks just like 0 unless you use a triple ===
Note that

$Under6MonthsLACode='00KC';

if ($Under6MonthsLACode == 0) echo "OK";

Open in new window


returns TRUE ('00KC' as number is converted as 0)
I think you might get better results if you separate the conditional statements according to your description, instead of trying to get them all to work together.  This is not tested and it does not account for the possibility that some of the variables might add up to zero if negative values were employed, but hopefully it illustrates a useful way to make several individual evaluations and let the outcome be represented by a single variable.
<?php // RAY_temp_eict.php
error_reporting(E_ALL);

/* THE DESCRIPTION OF THE ISSUE
This IF statement is supposed to check to see if
1. The $Under6MonthsLACode is set to 0
or
2. All of the $Under6MonthsLA... Variables (Years, Months or Days) are set to 0
or
3. All of the $ServiceAA... Variables (Years, Months or Days) are set to 0
if one of the above is true then set $HostDurationUnknown to 1.
*/

// INITIALIZE
$HostDurationUnknown = 0;

// TEST CONDITIONS
if ($Under6MonthsLACode == 0) $HostDurationUnknown = 1;
$x = $Under6MonthsLAYears + $Under6MonthsLAMonths + $Under6MonthsLADays;
if (!$x) $HostDurationUnknown = 1;
$x = $ServiceAAYears + $ServiceAAMonths + $ServiceAADays;
if (!$x) $HostDurationUnknown = 1;

Open in new window

Avatar of EICT

ASKER

Thanks Guys,
I gave more points to fraigor as he explained the reason behind it.