Mar 22, 2011
I have question about some behavior I was just debugging, specifically what happens if a variable which is already set is assigned to an undefined value. I just want to check that I'm understanding what happened correctly. If a variable has a value set already, and you try to set it to something undefined, it stays at its old value?
View 3 Replies
Specifically, I had some PHP code that looked approximately like this - assume that $string is some string of 1's and 2's.
Now, my code was supposed to set $string to be something like "12212", but an error on my part was sending it something with extra spaces at the end - "12212 ". This caused some aberrant behavior, and I think what happened was this - when $count=5, $string is undefined, so $array[$string] is undefined, and $newvar stays as 2. Thus my if(!empty statement doesn't do its job and case 'bar' happens more times than it should have. Does that all seem like what would happen?
Of course, trimming $string solved my problem, but I want to make sure I understand what was going wrong. Apologies if this is a stupid question - I'm just an amateur here....
Edit: Here's the actual code. $upstr is supposed to be a string of digits.
What happened when $upstr had some extra spaces at the end was I would see a mysql error, that it couldn't find the column "up" in $result2 . So it was trying to run that block of code in the if() statement with $name being empty or NULL or something. And if I intentionally added 3 or 4 extra spaces, I would see that many mysql errors.