Skip to main content
edited title
Source Link
Your Common Sense
  • 158k
  • 42
  • 227
  • 374

Why PHP casts two numerical strings to numbers before [loosely] comparing them?

I browsed through several similar questions, but they all only state the fact:

If ... comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

Okay, I got it. It explains what is going on when '00001' == '1' returns TRUE.

The question is: Why PHP does so?
What is the reason for probing strings for being numeric, and then casting if so? Why can't we just compare two strings already?

I can fairly understand what casting is required if two operands has different types. But why it does "usual math" when both are strings?

Why PHP casts two numerical strings to numbers before comparing them?

I browsed through several similar questions, but they all only state the fact:

If ... comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

Okay, I got it. It explains what is going on when '00001' == '1' returns TRUE.

The question is: Why PHP does so?
What is the reason for probing strings for being numeric, and then casting if so? Why can't we just compare two strings already?

I can fairly understand what casting is required if two operands has different types. But when both are strings?

Why PHP casts two numerical strings to numbers before [loosely] comparing them?

I browsed through several similar questions, but they all only state the fact:

If ... comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

Okay, I got it. It explains what is going on when '00001' == '1' returns TRUE.

The question is: Why PHP does so?
What is the reason for probing strings for being numeric, and then casting if so? Why can't we just compare two strings already?

I can fairly understand what casting is required if two operands has different types. But why it does "usual math" when both are strings?

Source Link
Your Common Sense
  • 158k
  • 42
  • 227
  • 374

Why PHP casts two numerical strings to numbers before comparing them?

I browsed through several similar questions, but they all only state the fact:

If ... comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

Okay, I got it. It explains what is going on when '00001' == '1' returns TRUE.

The question is: Why PHP does so?
What is the reason for probing strings for being numeric, and then casting if so? Why can't we just compare two strings already?

I can fairly understand what casting is required if two operands has different types. But when both are strings?