Php functions strcmp
, strcasecmp
and other from this family are binary-safe.
Are operators ==
and ===
binary-safe?
And if yes what should be used in binary-safe string comparison: functions or operator?
(binary-safe: "operator or function that can be applied to variables without altering their current state")
The most common way you will see of comparing two strings is simply by using the == operator if the two strings are equal to each other then it returns true. This code will return that the strings match, but what if the strings were not in the same case it will not match.
== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. === Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.
The term is mainly used in the PHP programming language to describe expected behaviour when passing binary data into functions whose main responsibility is text and string manipulating, and is used widely in the official PHP documentation.
A binary safe string is one that can consist of any characters (bytes). For example, many programming languages use the 0x00 character as an end-of-string marker, so in that sense a binary safe string is one that can consist of these.
They are, by your definition, but if you're comparing strings containing binary data, you should be aware of the fact that in PHP $a == $b
can return true even when $a and $b are different. Example: "2e2" == "200"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With