We help IT Professionals succeed at work.

php IF statement problem

EICT
EICT asked
on
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





Comment
Watch Question

I would have thought that this would have worked as is but if you change the comparison to this it works fine.
if (
$Under6MonthsLACode == '0'||
($Under6MonthsLAYears == '0' && $Under6MonthsLAMonths == '0' && $Under6MonthsLADays == '0')||
($ServiceAAYears == '0' && $ServiceAAMonths == '0' && $ServiceAADays == '0')
)          
     $HostDurationUnknown = 1;
    else $HostDurationUnknown = 0;
       
echo $HostDurationUnknown.'<br>';

Open in new window

this also works, doing like this === does a value and type comparison.
if (
$Under6MonthsLACode === 0||
($Under6MonthsLAYears === 0 && $Under6MonthsLAMonths === 0 && $Under6MonthsLADays === 0)||
($ServiceAAYears === 0 && $ServiceAAMonths === 0 && $ServiceAADays === 0)
)          
     $HostDurationUnknown = 1;
    else $HostDurationUnknown = 0;
       
echo $HostDurationUnknown.'<br>';
Developer
Commented:
In PHP some comparisions are not Literal.
so, if you compare "" or null with 0 it returns true.

$somevar=0;
if ($somevar==0) echo "Ok\n";
$somevar="";
if ($somvar==0) echo "Ok\n";
$somevar=null;
if ($somvar==0) echo "Ok\n";

Open in new window

The three ifs are returning true (3 Oks)


So, if it's the case, you need to use the literal comparision operator ===

$somevar=0;
if ($somevar===0) echo "Ok\n";
$somevar="";
if ($somvar===0) echo "Ok\n";
$somevar=null;
if ($somvar===0) echo "Ok\n";

Open in new window

In this case, only the first if it's true.

Chris Harte2015 Top Expert (Most Article Points)
CERTIFIED EXPERT

Commented:
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 ===
F IgorDeveloper

Commented:
Note that

$Under6MonthsLACode='00KC';

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

Open in new window


returns TRUE ('00KC' as number is converted as 0)
Most Valuable Expert 2011
Top Expert 2016

Commented:
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

Author

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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.