Identical sign "===" is only used in PHP? Why?

Asked

Viewed 364 times

9

During my studies in C and Java, I always came across the sign "=" of attribution and the sign "==" being of equality. However, I am studying PHP now and came across the sign "===" of identical.

My doubts are:

  • Is Identical Sign only used in PHP? If so, why?
  • If not, which languages have this sign?
  • If C and Java have it, why is it not so well known/taught along with the other signs?

1 answer

14


Is the identical sign used only in PHP? If not, which languages have this sign?

No, it’s used at least in Javascript also, perhaps in other languages of dynamic and weak typing.

The == compares the values in the best possible effort, if the types of each operand are different the language tries to make them compatible to be able to make the comparison. That can bring undesired results.

The === consider the type and no coercion is made. If the types are different it is already ensuring that the result will be different and the value is only evaluated if the types are equal.

If so, why?

PHP is a language of script, was made to "facilitate" the rapid creation of code and the programmer did not have to worry about certain details, so that made sense. Today language tries to stop being script, but you need to maintain compatibility, so you get this kind of schizophrenic thing.

If C and Java have it, because it is not so well known/taught along with the other signs?

This is characteristics of weak typing languages where a value can be passed as if it were of another type. Java is not like this.

C is like that, but in a slightly different way, usually you don’t try to do conversions, it just tries to access and raw memory. They never found it necessary to have an operator of "identical" in C because the philosophy of language is to let access anyway. But today almost all compilers, at least optionally, produce warnings when this occurs by forcing the programmer to use a cast to indicate that the intention is to use one type as if it were another.

It is very rare for a language to have static and weak typing, C is so because it is considered a portable assembly.

Even dynamic typing languages opt for strong typing, case of Python, Moon, Harbour, etc..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.