Debugging Subscription ProblemsI like to think of myself as a precise person, but the reality is that a lot of the time, things fall through the cracks.
I recently
(May 19 '22) discovered that the newsletter subscription feature I wrote for this site hasn't been working properly, ever since its introduction. I think I've fixed it now. If you used it before, there's a good chance you're going to have to subscribe again. (If, on the other hand, you sent me an email, I can restore your subscription based on that email.)
The error consisted of several parts, but I'm going to focus on one which shows how a bad understanding of a language can trip you up. I'm going to use some technical terminology, but I'll attempt to explain certain terms in the end notes.
Here's a sample to illustrate the problem.
if($list){
$target = array_search($entry, $list);
}
if(!isset($target)){
// do stuff to add entry to list
} else { // do stuff to remove entry from list }
Once again, here's a rough English equivalent.
1. If the variable "list" is true [1], then:
Let the value of "target" be equal to the value which is given by searching the "list" array for the value "entry": the key [2] of the value if found, or 'false' if not.
2. If "target" is not declared and is not different from null, then do stuff to add the entry to the list. Else, do stuff to remove the entry from the list.
You have likely figured out at least one problem, if you're a programmer. If not, read on to hear about one I chose to focus on for this post.
PHP can make loose comparisons or strict comparisons. For an analogy to help understand this, suppose that you're attempting to purchase a bag of chocolate chips online. When the store tries to package your order, they realize they're out of that specific brand of chocolate chips. Assuming it's impossible to get a bag of chocolate chips of the brand you requested, there's two ways they can handle this:
1) Find a bag of chocolate chips from another brand that appears to be equivalent, and substitute that for your order.
2) Leave the chocolate chips out of the order completely and explain that they were out of the brand you wanted.
1 is roughly equivalent to a loose comparison. It looks at the content, attempts to judge intent, and sends what convention states is most reasonable.
2 is a strict comparison. You made a specific request, and that exact item is not available, so your order can't be fulfilled.
Most of the time, most people will like the result of a loose comparison; you got something back and were helped, instead of not. On the other hand, there are times when you want something very specific. If the reason why you ordered that bag of that brand of chocolate chips is because they contained no vanilla and all the others do, you're not going to be happy with a substitution. Their guess was that you wanted a bag of chocolate chips, but you didn't: you wanted a bag of chocolate chips
made without vanilla.
Going back to our programming problem, two things you need to know at this point are that 1) PHP regards "false" and the number 0 as equivalent in loose comparisons, and 2) PHP counts starting from 0, not 1.
This means that with a loose comparison, the result of successfully finding the key of the first item in the list (0), and the result of not finding anything at all (false), are treated the same.
There were at least two other problems in my initial code, which I don't intend to expound on at length at this time. One of them involved my using "!isset" in the first place; another was with the "if($list)". Avoiding the false/0 equality problem described above wouldn't be enough by itself to resolve the issue. However, it is still a part of the solution, and they're all similar problems in that they reflect similar misunderstandings of the very specific meanings these terms have.
It's nice to get things right the first time. But sometimes you make a mistake. It's nice to completely fix something that's broken on the first attempt. But sometimes solutions to problems come a step at a time. Whether in programming, in writing, or in life, incremental improvement is better than none at all!
[1] Thanks to Michael Berkowski for his clear explanation of 'truthiness'.
[2] An array is a collection of variables, and a 'key' is the unique identifier of a variable in that array.
Basic Aspects of ProgrammingI've spent time around computers and/or programming, and sometimes I hear people say things to the effect that what I do is "complicated" or "over [their] head". What that seems to imply to me is, "I could never do that". In this article, I intend to show that not only
could you do that, you may already be thinking or doing things that overlap with the programming field.
... (read all)
Choosing a Popular OSThere are three families of operating systems for regular users in modern computing: macOS, Windows, and Linux (which includes Google’s Chrome OS).
... (read all)
The Second-Person Plural PronounThe true shame in modern English's lack of a second-person plural is that the damage is self-inflicted.
... (read all)
Virtual Dice GamesI suppose it is appropriate that my first working demo of object-oriented programming would be a game.
... (read all)
Too Quick for You"Users don't always want things to work faster"?
... (read all)
Introducing gPressWelcome to the first public post from gPress! I didn't expect to return to blogging again, but circumstances have made it compelling, and if I'm going to blog, I want to do it with a platform that suits me.
... (read all)