Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. PHPBeginner - What are Logical Operators?

What are Logical Operators? - Images: Japanese

Hey there, everyone!

Next up, let's take a look at logical operators. These are operators that are commonly used alongside the if statement we covered last time. The idea is something like "if this AND that" or "if this OR that". You'll use these constantly, so let's get them down solid.

There are four logical operators: &&, ||, xor, and !.

On a US keyboard, you can type | by holding Shift and pressing \. On a JIS (Japanese) keyboard, it's Shift + .

Let's start with &&. It's called the logical AND operator. In practice, most developers just call it "double-and" or simply "and".

Here's how you use && together with an if statement:

<?php
if(true && true){
    var_dump('Executed!');
}

Notice the () part of the if statement — it says true && true. && evaluates to true only when both operands (both sides) are true. In the example above, both operands are true, so the if statement runs. Now try this instead:

<?php
if(true && false){
    var_dump('Executed!');
}

When one side is false, it won't run. Same goes for false && false. && is often described as "this AND that" in textbooks. If you ever forget how it works, searching for "if statement AND condition" should set you straight.

Next up is ||. It's called the logical OR operator. This one evaluates to true when at least one of the operands (either side) is true.

<?php
if(true || false){
    var_dump('Executed!');
}

Here we have true || false — one side is true, so the if statement runs. Now take a look at this:

<?php
if(false || false){
    var_dump('Executed!');
}

This time it's false || false — both sides are false, so it won't run. By the way, it doesn't matter which side is truefalse || true and true || true both run just fine. Easy to get tripped up, so watch out.

Next is xor. This is called the exclusive OR operator. Sounds fancy, but it's straightforward — xor only runs when exactly one side is true. Here's an example:

<?php
if(true xor false){
    var_dump('Executed!');
}

Exactly one side is true, so it runs. But true xor true and false xor false will NOT run. The only patterns that run are true xor false and false xor true.

Finally, let's look at '!'. It's called the 'logical NOT operator'. Note that the '!' symbol itself (used just as punctuation) can be called an "exclamation mark", "bang", or "exclamation point" — the "logical NOT operator" label only applies when it's used as a logical operator. Here's how you use it:

<?php
if(!false){
    var_dump('Executed!');
}

Take a closer look at !false. There's a ! stuck right in front of false.

! is a bit unique — it only takes one operand. While && and || evaluate two operands on the left and right, ! only works with a single operand on its right side.

! evaluates its operand as a boolean, then flips the result.

Let's recall from a previous article which values are treated as true and which are treated as false. Everything in the list below is treated as false — anything not on this list is true:

  • The boolean value false
  • The integer 0
  • The float 0.0
  • An empty string ('' or "") and the string '0'
  • An empty array (including associative arrays)
  • NULL

For example, the integer 1 in PHP is treated as true. So writing !1 gives you back false. Like this:

<?php
var_dump(!1); // Outputs 'false'

Now let's attach '!' to an empty string '' and write '!'''. An empty string is treated as 'false' in PHP, so flipping it should give us 'true', right?

Here's the result:

<?php
var_dump(!''); // Outputs 'true'

That's exactly what ! does — it evaluates the operand as a boolean and returns the flipped value. It comes up often, so it's worth remembering.

&& can be rewritten as and, and || can be rewritten as or.

<?php
if(true and true){ // Same as writing 'if(true && true)'
    var_dump('Executed!');
}

if(true or false){ // Same as writing 'if(true || false)'
    var_dump('Executed!');
}

In practice, '&&' and '||' seem to be the more popular choice.

And that's it for this article!

Next, we'll cover the switch statement. Once we're done with that, we'll write a small program to put everything together — that'll be a great chance to see how logical operators and if statements work in a real context.

See you in the next one!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .