Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is === in PHP binary-safe in string comparison?

Tags:

php

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")

like image 520
rdo Avatar asked Jul 25 '12 12:07

rdo


People also ask

Can you use == to compare strings in PHP?

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.

What is the difference between == and === comparison operators in PHP?

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

What is binary-safe in PHP?

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.

What is binary-safe string?

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.


1 Answers

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"

like image 165
Anonymous Coward Avatar answered Oct 19 '22 15:10

Anonymous Coward