Setting
I left PHP to venture into the world of C# and felt an immense difficulty to understand a little bit about typography. In fact, it took me a while to understand why I couldn’t compare string with null to do something if the string was empty.
After a while pondering the problem in the head, I fell deep into the internet and ran after. Lo and behold the solution comes next.
What is "0"?
Zero is zero and dot. 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 equations mean exactly the same. Which, in turn, means content/value and guy equal.
On first parole, we have true
as a comment because $n
is an integer/number, and its value is 0
. That is, the content And typing is the same.
On the second parole, the comment is false
because he compares $n
with '0'
, zero being, in this case, a string.
Briefly, number is a jealous 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 they have any fillers, you use the Empty to make the comparison.
In short, it checks whether a string has content; whether or not it is empty.
What is the "null"?
null means null. You compare null when you know that a variable has the chance to possibly not bring anything.
They want a practical example?
<?php
class Cachorro
{
public $estado;
public function sentar()
{
if ($this->estado != 'sentado')
$this->estado = 'sentado';
}
}
As we can see, I created a class Cachorro
and a method sentar
. Before the dog sits, we check his condition. If he is already seated, he will do nothing, that is to say, the function nay will return nothing (null
); otherwise it will alter your estado
for 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 anything to you beyond waiting for a next command (null) or perhaps a cookie for having performed such a masterful task.
Heed: the term null
does not have only conceptual or didactic purposes. null
also specifies that a variable is not allocated in memory. For example, if we have a variable $x = 0
and then we moved on to $x = null
, then we made a data offset. In other words, we removed information from memory.
And finally, what is "false"?
If someone asks you if you are hot most of the time, there are two answer options: true
or false
. You probably won’t talk 0
or you’ll stop answering (empty
). You will say yes or nay. And that’s exactly what the true
and false
sane.
<?php
class Cachorro
{
// ...
public function latir()
{
echo 'Woof!';
return true;
}
}
In the above example, the method latir()
will return true
.
<?php
if ($cachorro->latir())
echo 'O cachorro latiu!';
else
echo 'O cachorro não latiu. :(';
And according to the above parole, two things will be displayed:
- 'Woof!'
- 'The dog barked!';
Joining together:
- 'Woof! The dog barked!';
Why does this happen?
Now, in the if
you are running a function and checking if her return is true. If it is, display O cachorro latiu!
. And is it true?... Of course it is! This is explicit in return
of the method latir()
class Cachorro
.
null
vs. false
, empty
and 0
Taking the previous example, but removing the return
:
<?php
class Cachorro
{
// ...
public function latir()
{
echo 'Woof!';
}
}
What kind will be latir()
will return? Bye bye bye bye bye
And the guy will be.............. null!
To make a comparison of nulls in practice, follow the model:
if ($cachorro->latir() == null)
// faça algo
William, would you please edit the question so that it sounds like a question, and not as an introduction to the answer? I believe it will work better on our platform. Ah, and it’s totally valid to ask and answer your own question; you don’t need to mark the answer as wiki, it’s fair to earn points for it.
– bfavaretto
@bfavaretto Ready, my dear. Thanks for the tips!
– Guilherme Oderdenge
We could consider Empty to be a space?
– marco
A good tip (as always) is the official documentation. This Appendix of the documentation shows how the different variable test functions work on different values with different types. http://www.php.net/manual/en/types.comparisons.php
– user2171