In the Arithmetic module, we touched on the topic of weak typing.
PHP is a weakly typed language. It recognizes different data types (numbers, strings, etc.), but doesn't use them too strictly, it tries to convert data when it seems reasonable.
A particularly large number of automatic transformations take place when working with logical operations.
In PHP there are two simple rules by which conversions take place:
0
, '0'
, ''
(empty string), null
are set to false
. These values are called falsy.true
.This works the other way as well: true
and false
are converted to other data types, depending on the situation:
<?php
print_r(true);
1
<?php
print_r(false);
(an empty string has been printed)
Another example:
<?php
print_r(0 || 1);
1
What happened here:
The OR operator only works with the bool type, but it's also given the numbers 1
and 0
.
0
is converted to false
and 1
is converted to true
.false || true
is true
.print_r()
gets true
, but it only works with strings (not bool).true
is converted to 1
.1
.In one of our lessons, we looked at the comparison operators ===
and !==
and mentioned that PHP also has the operators ==
and !=
, but we shouldn't use them. The difference lies in type conversion:
<?php
var_dump('' === false); // => false
var_dump('' == false); // => true
An empty string and false
are different values, so the ===
operator says "false! they're not equal!
But the ==
operator converts types, making an empty string and false
equivalent from its point of view. This conversion is implicit, so avoid the ==
and !=
operators whenever possible.
Remember the negation operator:
<?php
$answer = true;
var_dump(!$answer); // => false
When you use double negation !!
the result value is equal to the initial value:
<?php
$answer = true;
var_dump(!!$answer); // => true
However, this is where type conversions also take place. Therefore, the result of a double negation will always be a bool. This trick is used to change the data type.
Different programming languages have different transformation rules. Some languages don't convert types themselves at all. Many features of PHP aren't really the best, but this is the historical legacy of the language. If it were created from scratch today, many of the rules and nuances would probably be quite different.
Write an isFalsy()
function that checks if the passed value is interpreted as false from PHP's point of view. To do this check, you need to compare false
with the passed value using ==
.
<?php
isFalsy(false); // true
isFalsy(0); // true
isFalsy('help'); // false
isFalsy(10); // false
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
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.
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.
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.
Your exercise will be checked with these tests:
1<?php
2
3namespace HexletBasics\Logic\WeakTyping;
4
5use PHPUnit\Framework\TestCase;
6
7class Test extends TestCase
8{
9 public function test()
10 {
11 require 'index.php';
12
13 assert(isFalsy(''));
14 assert(!isFalsy(' '));
15 assert(isFalsy(null));
16 assert(isFalsy(false));
17 assert(!isFalsy(true));
18 assert(!isFalsy(3));
19 }
20}
21
Teacher's solution will be available in: