Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strange happenings with &&

Status
Not open for further replies.

dkemas

Programmer
Mar 22, 2012
70
0
0
GB
I am checking that all sections in a database have been completed, if each has then set a variable to say it is complete. I then check that all of those variables have been set as complete, if they have then open up some content on a page.

I am getting a problem though where my final if statement is saying sections are complete when they aren't.

Here's what I am doing

Code:
//to start with set all as incomplete
$p1class = 'incomplete';
$CLINclass = 'incomplete';
$PCclass = 'incomplete';
$SGclass = 'incomplete';
$IPCclass = 'incomplete';
$RSclass = 'incomplete';
$CFclass = 'incomplete';
$PERFclass = 'incomplete';

//check to see if each section is complete
$queryp1 = my query
$num = $dbStuff->FetchNum($queryp1[0]);
if check complete {
$p1class = 'section-complete';
}

//repeat for all sections above. So now my variables are either set as 'section-complete' or 'incomplete'
//echoing these to the page correctly show complete or incomplete for each section, so far so good
//this is where things are going wrong

//now check that all sections are complete
if (($p1class) && ($CLINclass) && ($PCclass) && ($SGclass) && ($IPCclass) && ($RSclass) && ($CFclass)  <> 'section-complete' ) {
$wohitsnotcomplete = 'STOP';
} else {
$wohitsnotcomplete = 'GO';
}

//now when I 
echo $wohitsnotcomplete;
//even if some are incomplete it is printing as GO

I have tried the final if statement with just each variable in turn and each work, it is when I look at them all together that I have the problem.

My problem must lie with the final if statement and the && operator but I have read every resource I can find on && and it loks to be ok. Can someone see a problem somewhere?

Thanks
 
Scrap this post, it was the brackets around my variables, I removed those and it worked. I am positive I had tried doing that before without luck though.

Thanks
 
was it? it looked to me that your logic was incorrect. the long statement reads as

if EACH OF THESE THINGS does not equal section-complete then print 'stop'.
otherwise print 'go'

however because plclass is potentially set to section-complete then it will always print go.

so perhaps your logic should be using the logical OR (||) rather than the logical AND (&&)

also this code block should have thrown a fatal error
Code:
if check complete {
$p1class = 'section-complete';
}

the correct syntax for the IF construct is
Code:
if [red][b]([/b][i]some expression[/i][b])[/b]{
  //do some action
} [ else {
  //do some other action
}][/red]

your expressions are missing brackets around the conditional expression and the expression itself appears invalid,
 
I would think it is your if statement. The way I read that is:

Code:
if $p1Class is true AND
   $CLINclass is true AND
   $PCclass is true AND
   $SGclass is true AND
   $IPCclass is true AND
   $RSclass is true AND
   $CFclass is not 'section-complete' then $wohitsnotcomplete = 'stop';

You should be checking EACH variable and as jpadie said using the OR operator:
Code:
if $p1Class is not 'section-complete' OR
   $CLINclass is not 'section-complete' OR
   $PCclass is not 'section-complete' OR
   $SGclass is not 'section-complete' OR
   $IPCclass is not 'section-complete OR
   $RSclass is not 'section-complete' OR
   $CFclass is not 'section-complete' then $wohitsnotcomplete = 'stop';

Also in PHP while <> is accepted for "not equal" you'll find it more commonly expressed as !=.
 
I agree with Borvik.

&& and || separate statements, not variables to be evaluated.

Each statement is evaluated on its own. So basically the only one that gets checked against 'section_complete' is the last variable. All the other ones are being checked only for existence. Doesn't matter what they contain as long as they exist.

Take this shorter example:

Code:
$var1="stop";
$var2="stop";
$var3="stop";
$var4="go";

if($var1&&$var2&&$var3&&$var4!='stop')
{
	echo "All Go";
}
else
{
	echo "Some Stops";	
}

3 out of the 4 variables are set to stop, yet the IF still prints "All Go". It will only print stop if $var4 is set to stop.

Same thing happens with your evaluation, only the last variable is actually being checked against the 'section_complete' value.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top