Scenario
I left PHP to venture into the C # world and felt a tremendous difficulty to understand a bit about typing. In fact, it took me a while to understand why I could not compare a string with null to do something if the string was empty .
After a while of the problem in the head, I fell into the background on the internet and ran after. Here is the solution that follows.
What is "0"?
Zero is zero and period. Who can be zero is a number, not a letter or an empty space. Zero is zero and end of chat.
In PHP:
$n = 0;
if ($n === 0)
// true
if ($n === '0')
// false
To explain: the three adjacent equalities mean exactly the same . That in turn means content / value and type .
In the first conditional, we have true
as comment because $n
is an integer / number, and its value is 0
. That is, the content E typing are the same.
In the second conditional, the comment is false
because it compares $n
with '0'
, where zero is a string in this case.
In short, number is a ciosa and string is another. Although they are two zeros, they are distinct types .
What is the "empty"?
When you have a variable of type string and want to check if it has any fill, you use empty to do the comparison.
In short, it checks whether or not a string has content; whether or not it is empty.
What is "null"?
null means null . You compute null when you know that a variable has the chance of possibly bringing nothing.
Want a practical example?
<?php
class Cachorro
{
public $estado;
public function sentar()
{
if ($this->estado != 'sentado')
$this->estado = 'sentado';
}
}
As you can see, I created a Cachorro
class and a sentar
method. Before the dog sat down, we checked his condition. If you're already seated, it will not do anything , that is, the no function will return nothing ( null
) ; otherwise, it will change its estado
to sentado
.
To make it easier, if a trained dog wins the order to sit down when he is already seated, he will probably continue that way and will not return you anything other than waiting for a next command ( null ) or perhaps a cookie for having performed a task so masterfully.
Attention: The term null
is not just for conceptual or didactic purposes. null
also specifies that a variable is not allocated in memory. For example, if we have a $x = 0
variable and then change to $x = null
, then we have done a deallocation of data. In other words, we removed an information from memory.
And finally, what is "false"?
If someone asks you if you are hot, most of the time, there are two options for answer: true
or false
. You probably will not talk 0
or will stop responding ( empty
). You will say yes or no . And that's just what true
and false
are.
<?php
class Cachorro
{
// ...
public function latir()
{
echo 'Woof!';
return true;
}
}
In the example above, the latir()
method will return true
.
<?php
if ($cachorro->latir())
echo 'O cachorro latiu!';
else
echo 'O cachorro não latiu. :(';
And according to the conditional above, two things will be displayed:
'Woof!'
'The dog barked!';
Putting together:
'Woof! The dog barked!';
Why does this happen?
Now in if
you are executing a function and checking if returns true . If it is, display O cachorro latiu!
. And is it true? ... Of course! This is explicit in return
of method latir()
of class Cachorro
.
null
vs. false
, empty
and 0
Taking the previous example, but removing return
:
<?php
class Cachorro
{
// ...
public function latir()
{
echo 'Woof!';
}
}
What kind of latir()
will return? Tchã tchã tchã tchã
And the type will be .............. null !
To make a comparison of nulls in practice, follow the model:
if ($cachorro->latir() == null)
// faça algo