Free PHP course. Sign Up for tracking progress →

PHP: Weak Typing - 2

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.
  • Everything else becomes 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.

  1. 0 is converted to false and 1 is converted to true.
  2. The result false || true is true.
  3. Now print_r() gets true, but it only works with strings (not bool).
  4. true is converted to 1.
  5. And the screen displays 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.

Instructions

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
The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • 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.

Tips