0

I know that this is caused by type conversion, but can someone explain me, what exactly is happening here, when:

(2 == '2abd') // true
(3 == '3a') // true

I was a bit surprised when running into a bug by comparing a string variable against strings and integers.

Thank you very much!

c1u31355
  • 420
  • 5
  • 10

1 Answers1

0

If we convert 2abd and 3a to integers, you'll get 2 and 3 accordingly.

Since 2 == 2 and 3 == 3 both statements return true.


Online test case:

<?php

var_dump(2 == '2abd');  // true
var_dump(3 == '3a');    // true

var_dump((int) '2abd'); // 2
var_dump((int) '3a');   // 3

var_dump(2 == 2);   // true
var_dump(3 == 3);   // true

For more info about operator precedence, please take a look at:

0stone0
  • 34,288
  • 4
  • 39
  • 64