compare an empty string to one with a value. why worketh?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

compare an empty string to one with a value. why worketh?

Post by Burrito »

Lets say I have 20 rows on my database created by 4 different users. I am trying to group the rows created by user and output them on an html table with alternating colors per user.

so I might have one user (user a) with 12 rows, another (user b) with 4 rows, another (user c) with 3 rows and finally another (user d) with 1 row.

I was able to get it working, but I'm a bit baffled as to why it does work...I did something like this:

Code: Select all

$color = TRUE;
$user = "";
while($row = mysql_fetch_assoc($result)){
  if($user !== $row['user'])
     $color = !$color;
  echo ($color ? "blue" : "red");
}
as I said, it works just great, but I don't understand why. I'd think the first row would be different (for the first user) than the rest of his/her rows...then work fine for the remaining users.

why does my first comparison result in true? I'm assuming that php has something built in to ignore empty strings when using !== as a comparison? Perhaps if I used !=, I'd get the result I expected? I would try it, but I'm home now and am too lazy to recreate it here, I'll try tomorrow at work.

if someone can shed some light as to why this DOES indeed work, I'd most greatly appreciate it.

thx,

Burr
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Why don't you try var_dump'ing each variable in the comparison?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

what is the very first colour that appears?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

well in that little sample it would be blue, but the weird thing is, it stays blue for the next 11 iterations (until the next person comes up in the while loop). That's why I'm confused. I'd think that the first one would be red (the two strings don't match) and then the next 11 would be blue, then alternate back to red for the next user and so on, but such is not the case. It works exactly like I WANT it to, but not how I UNDERSTAND that it should...thus my post.

edit: just re-read this...a little confusing: let me clarify

the reason I think the the second through the 12th should be a different color from the first, is that the $user var doesn't get set until after the if, at which point it gets set to the first person's name so the 2nd through the 12th should work out right as the strings match, but that first one is the confusing one...hope that makes more sense
Last edited by Burrito on Wed Jun 22, 2005 12:53 am, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

doh! sorry for the second post here...long day for me. Just re-read the initial post and realized I missed something in the code snippet I posted:

Code: Select all

$color = TRUE;
$user = "";
while($row = mysql_fetch_assoc($result)){  
  if($user !== $row['user'])     
    $color = !$color;  
  $user = $row['user']; // missed this above
  echo ($color ? "blue" : "red");
}
there...that should make a little more sense now, sorry about that oversight.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

$color = !$color;

I think this line is what is causing the confusion.

What I think is happening is when then this first compares it is going is color not equal to color, instead of doing the assingment that you are expecting.


the only other thing to do is to put an extra echo inside the if statment to see if you get a respone on the first line, because the !== will also check it they are vars are the same type, so maybe they are both strings and that satifies it?

last bit is grasping at straws.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

tried the var_dump() I did.

revised code (real code with var_dump() added)

Code: Select all

$cla = TRUE;
$creby = "";
while($gtstuff = mysql_fetch_assoc($getstuff)){
var_dump($creby);
var_dump($gtstuff['createdby']);
if($creby !== $gtstuff['createdby'])
	$cla = !$cla;
$creby = $gtstuff['createdby'];
var_dump($creby);
yields this it does:
string(0) "" string(1) "1" string(1) "1"
string(1) "1" string(1) "1" string(1) "1"
string(1) "1" string(1) "1" string(1) "1"
string(1) "1" string(1) "2" string(1) "2"
string(1) "2" string(1) "2" string(1) "2"
string(1) "2" string(1) "3" string(1) "3"
still very confused I am. the output above expected I did, leaning toward your $cla = !$cla idea Scott I am.

hate voodoo solutions I do, know what is really going on here I want!
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

OK, the first time through the loop, it sees that the row-based user doesn't match your "currentUser" variable, so it toggles the color. It also sets currentUser tracker to the row-based user.

The next time through the loop the currenttUser and the row-based on match, so nothing is toggled and the color remains the same. The first time they differ again, the comparison succeeds so the color is toggled agaain.

Note, you can slightly optimize the code

Code: Select all

$color = TRUE;
$user = "";
while($row = mysql_fetch_assoc($result)){  
  if($user !== $row['user'])
   {     
     $color = !$color;  
     $user = $row['user'];
   }
  echo ($color ? "blue" : "red");
}
saving yourself a few needless re-assignments. (Not a big deal, but this is such a common code pattern, that it just looks wrong to me without it.)


What you seem to be expecting would be this:

Code: Select all

$user = "";
while($row = mysql_fetch_assoc($result)){  
  $color = ($user !== $row['user'])
  $user = $row['user'];
  echo ($color ? "blue" : "red");
}
That is, show one color if they don't match, show another color if they do match. This would always be wrong on the first row of a block, not just the first. Both code snippets have their use; I think you wanted what you wrote, but expected the latter.

Just remember that $color is a toggle-type variable, not a direct result of the comparison.
Post Reply