Page 1 of 1

similar both equal true?

Posted: Thu Nov 10, 2005 6:15 am
by blacksnday
How do you make the following work?

$var = 123.456
$var2 = 123.


and both return a positive match for something like

$test = 123

Posted: Thu Nov 10, 2005 6:33 am
by shiznatix
preg replace the . and everything after it for both variables. then just do a simple check with == . sorry i don't know the regex for that but thats how it would be done

Posted: Thu Nov 10, 2005 6:34 am
by blacksnday
thanks.
thats what I was thinking, but wanting to confirm :)

Posted: Thu Nov 10, 2005 6:35 am
by redmonkey

Code: Select all

<?php

$var1 = '123.456';
$var2 = '123.';

echo ((int) $var1 == (int) $var2) ? 'ture' : 'false';

?>

Posted: Thu Nov 10, 2005 6:37 am
by shiznatix
i have seen the use of (int) do some crazy things which makes me not trust php when it comes to defining the variable type manually

Posted: Thu Nov 10, 2005 6:41 am
by Jenk
The 'crazy' stuff I have seen is numbers (assumed ints) with leading zeros. Which infact are not int's as int's don't have leading zeros :P

eg:

Code: Select all

<?php

echo (int) 0123; //outputs '83' due to 0123 being octal, not decimal.

echo (int) 012; //outputs '10' 

?>

Posted: Thu Nov 10, 2005 6:46 am
by redmonkey
shiznatix wrote:i have seen the use of (int) do some crazy things which makes me not trust php when it comes to defining the variable type manually
I've seen a car catch fire for no apparent reason, but I still drive.

It may well be that you uncovered a bug, or it may have been a case that type casting was doing something you didn't fully understand.

Posted: Thu Nov 10, 2005 6:51 am
by redmonkey
Jenk wrote:The 'crazy' stuff I have seen is numbers (assumed ints) with leading zeros. Which infact are not int's as int's don't have leading zeros :P

eg:

Code: Select all

<?php

echo (int) 0123; //outputs '83' due to 0123 being octal, not decimal.

echo (int) 012; //outputs '10' 

?>
This is expected behavoiur. As you point out, you are type casting an octal number. What would you expect?

On the other hand..

Code: Select all

echo (int) '0123'; // casting from string to integer gives 123

Posted: Thu Nov 10, 2005 6:52 am
by Jenk
redmonkey wrote:
Jenk wrote:The 'crazy' stuff I have seen is numbers (assumed ints) with leading zeros. Which infact are not int's as int's don't have leading zeros :P

eg:

Code: Select all

<?php

echo (int) 0123; //outputs '83' due to 0123 being octal, not decimal.

echo (int) 012; //outputs '10' 

?>
This is expected behavoiur. As you point out, you are type casting an octal number. What would you expect?

On the other hand..

Code: Select all

echo (int) '0123'; // casting from string to integer gives 123
I would expect exactly what it produced.. but I was pointing out that what others may look at as odd, is not odd at all.

Posted: Thu Nov 10, 2005 7:24 am
by blacksnday
redmonkey wrote:

Code: Select all

<?php

$var1 = '123.456';
$var2 = '123.';

echo ((int) $var1 == (int) $var2) ? 'true' : 'false';

?>
fixed typo in quote...it was killing me... :oops:

thanks for that! thats awesome and actually
solves about 5 problems in one for me :D