# PHP: Logical Operators

Logical expressions can be combined with each other, creating increasingly tricky checks. One good example is password verification. As you'll likely know, many websites want a password of 8 до 20 characters on signup. Frankly, it's a weird restriction, but whatever, it is what it is. In mathematics, we'd write `8 < x < 20` (where `x` is the length of a particular password), but in PHP, this won't work. We would have to make two separate logical expressions and connect them with the special «AND» operator:

``````A password longer than 8 characters **AND** a password shorter than 20 characters.
``````

Here's a function that takes a password and says whether it meets the conditions or not:

``````<?php

{
return \$length > 8 && \$length < 20;
}

``````

`&&` means «AND» (called 'conjunction' in mathematical logic). The whole expression is true only when every operand, all of which are part of the compound expression, is true. In other words, `&&` means «both». This operator's priority is lower than that of comparison operators, so the expression works correctly without parentheses. Another widespread operator alongside `&&` is `||` — «OR» (disjunction). It means "one or the other, or both". Operators can be combined in any number and any sequence, but when `&&` and `||`, appear together, you should label priority with parentheses.

Below is an example of an advanced function that validates a password:

``````<?php

function hasSpecialChars(\$str)
{
// checks to see if there are special characters in the string
}

{
// The parentheses set the priority, making it clear how each part is related
return (\$length > 8 && \$length < 20) && hasSpecialChars(\$password);
}
``````

Another example. We want to buy an apartment that meets these conditions: an area of 100 square meters or more on any street OR an area of 80 square meters or more, but on `Main Street`.

We'll write a function that checks the apartment. It takes two arguments, the area (a number) and the street name (a string):

``````<?php

function isGoodApartment(\$area, \$street)
{
return \$area >= 100 || (\$area >= 80 && \$street === 'Main Street');
}

var_dump(isGoodApartment(91, 'Queens Street'));  // => false
var_dump(isGoodApartment(78, 'Queens Street'));  // => false
var_dump(isGoodApartment(70, 'Main Street'));    // => false

var_dump(isGoodApartment(120, 'Queens Street')); // => true
var_dump(isGoodApartment(120, 'Main Street'));   // => true
var_dump(isGoodApartment(80, 'Main Street'));    // => true
``````

The area of mathematics dealing with logical operators is called Boolean algebra. The truth tables are shown below, and they can be used to figure out the result of an operator:

## `&&`

A B A `&&` B
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE

A couple of examples:

``````<?php

// true && true;
3 > 2 && str_starts_with('wow', 'w'); // true

// true && false;
'start' === 'start' && 8 < 3; // false
``````

## OR `||`

A B A `‖` B
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE

A couple of examples:

``````<?php

// true || true;
3 > 2 || str_starts_with('wow', 'w'); // true

// false || false;
'start' === 'Start' || 3 < 3; // false
``````

## Instructions

Implement the `isLeapYear()` function, which determines whether the year is leap year or not. A leap year is a multiple of 400 (i.e. divisible without a remainder), or is both a multiple of 4 and not a multiple of 100. As you can see, the definition already contains all the required logic, all we need to do is to put it into code:

``````<?php

isLeapYear(2018); // false
isLeapYear(2017); // false
isLeapYear(2016); // true
``````

You can check if a number is a factor of another number like so:

``````<?php

// % - returns the remainder of the left operand divided by the right operand
// Checking that the number is a multiple of 10
\$number % 10 === 0
// Checking that the number is not a multiple of 10
\$number % 10 !== 0
``````
The exercise doesn't pass checking. What to do? 😶

• Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.

## Definitions

• Logical operators the «AND» (&&), OR (||), operators, which allow you to create composite logical conditions.

If you got stuck and don't know what to do, you can ask a question in our community