![]() We have three checks all will return true. As a result, one in integer form will equal one in a string format. That is because they do not consider the data type of the compare variables. The loose equality check in PHP uses the double equality sign = and not the equal sign != sign. I’m going to be assuming there exists a PHP function called do_thing, and your question across all our scenarios is not to worry what do_thing does–nothing, it’s a fake function–but just tell me if it will be called or not.This article will explain how to perform loose and strict equality checks in PHP.įor the loose equality checks, you’ll use some built-in check operators like the equality operator = and the not equal operator !=.Īlso, for the strict equality checks, you’ll use the strict versions of these operators. The bang operator ( !) is pretty easy, so we’ll start there. WordPress conditional tags are generally very easy to read, so if you’re not familiar this shouldn’t prove a big obstacle. We’re going to use a simpler situation to understand PHP conditional logic first, but then we’ll layer in some WordPress conditional tags. Examples of Using PHP Operators: ! just & and || in strange placesĮnough with the highfalutin words, let’s see some code. What this means is that “the sun equals the moon” is a false statement, but “the sun not equals the moon” is a true statement. It’s important to know that “not” ( !) in logical syntax doesn’t “turn it off” but instead “flips it.” So “not true” becomes false, and “not false” becomes true. Unlike with & and ||, there exists no linguistically-written version. The last thing you need to know to claim you have a robust boolean language is the presence of the “not.” In PHP, you’ll only write that with a “bang” or “exclamation point,” !. It means “if the things that surround me are both true, and never else.” Unless you’re looking to impress people, I’d avoid this, as people aren’t in the habit of reading it.) Bang ( !) is NOT to be forgotten It’s one I’ve never seen outside of tutorials. This is the rarest PHP logical operator of all. (Even more rare, there exists an “exclusive or” operator in PHP, written xor. Again, I didn’t realize until writing this article that or was valid PHP, so you should get used to reading and writing ||. What we see most frequently is instead the “double pipe” || operator. Like I said, in WordPress themes that might be “if this is a single post entry, or if it’s an archive page.” And like with and above, you can actually write valid PHP code with a simple: or where you mean that.īut, just the same, we almost never see that in the wild. Now for OR or ||: An Alternative Logical OperatorĪnother core idea of boolean logic with PHP is “or.” Sometimes there are two preconditions, where either being satisfied makes a thing acceptable to us. Long story short: use & when you want to say and in PHP code. ![]() The issue is that and behaves a little differently than most of us think, so it might surprise you from time to time in it’s execution. In fact, I can count on one hand the times I’ve read PHP that used and (or or or xor), and many of them were while I was writing this article. Why was I surprised that you can say “and” with both the & operator and the keyword and? Because no PHP code I’ve ever read has used the latter, and, format. It evaluates a little different, but the language lists it prominently in its documentation on logical operators. In fact (as someone who’s been writing PHP for 15 years, this surprised me) you can write and in PHP. These two pretzels serve the same role of and in some other programming languages you might be used to. In short, you say a PHP “and” with a double ampersand symbol ( &). And to understand that syntax, we must know how to say “and” and “or”, so let’s get to it. So a lot of WordPress PHP code ends up simply being rules like ‘if the data I’m showing is recent, and I’m showing a “page” post type, don’t show the date on the page.’ These rules must be written in a much more syntactically exact way than we speak them, but the principle is the same. ![]() (That’s the role of the “template hierarchy” in WordPress, if I just blew your mind.) At the risk of oversimplifying, in WordPress we simply store specific data, and then build simple little machine (called “WordPress themes”) to display that data to specific people in specific way under specific conditions. To the surprise of many people afraid of programming, a lot of what you do when programming is create and solve simple logic puzzles. Why PHP Logical Operators Matter & What They Are
0 Comments
Leave a Reply. |