0

Colleagues, I use the code below and it returns TRUE (!!!). How is that?

<?php
$var       = 'A string';
$varCasted = (int)$var;
var_export($varCasted == $var, 0);

Could you reproduce the same and explain me if you see the same? JFYI: it works if only I do:

var_export((string) $varCasted == (string) $var, 0);
  • @JohnConde. My question is marked as duplication but id does not have any common with questions it is merged!!! – Vsevolod Azovsky Jun 06 '20 at 11:48
  • Yes it does. They explain *exactly* what is happening here. If you do not understand the answers in those question, you will not understand any explanation given here. But, any non-empty string is going to be equal to any number when you use the `==` operator. If you use `===` you will see a different result. Understanding the three equal operators and type juggling PHP is *important*. Study those answers until you know them well. – John Conde Jun 06 '20 at 11:50
  • Sir, with all respect, Sir: you are wrong, saying "But, any non-empty string is going to be equal to any number when you use the ==". Just replace in my example $varCasted with a number and result becomes false. In addition: I do understand difference and have read all the related topics. Here I meet a bug and just am wondering if other guys see the same or it is something local related to say Open Server Panel. – Vsevolod Azovsky Jun 06 '20 at 12:11
  • You are converting to `int` and the expression is converting to `bool` and the behaviour for those are different – YouriKoeman Jun 06 '20 at 13:48
  • `(bool)` checks for empty strings where `(int)` checks for the presence of a numeric (with numbers only) string – YouriKoeman Jun 06 '20 at 13:50
  • Thanks for the response.You know... I guess, I understand. In compare expression String is transformed to a number non explicitly. After 10 years of programming basic things are forgotten. – Vsevolod Azovsky Jun 07 '20 at 09:25

0 Answers0