similar both equal true?
Moderator: General Moderators
- blacksnday
- Forum Contributor
- Posts: 252
- Joined: Sat Jul 30, 2005 6:11 am
- Location: bfe Ohio :(
similar both equal true?
How do you make the following work?
$var = 123.456
$var2 = 123.
and both return a positive match for something like
$test = 123
$var = 123.456
$var2 = 123.
and both return a positive match for something like
$test = 123
- blacksnday
- Forum Contributor
- Posts: 252
- Joined: Sat Jul 30, 2005 6:11 am
- Location: bfe Ohio :(
Code: Select all
<?php
$var1 = '123.456';
$var2 = '123.';
echo ((int) $var1 == (int) $var2) ? 'ture' : 'false';
?>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 
eg:
eg:
Code: Select all
<?php
echo (int) 0123; //outputs '83' due to 0123 being octal, not decimal.
echo (int) 012; //outputs '10'
?>I've seen a car catch fire for no apparent reason, but I still drive.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
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.
This is expected behavoiur. As you point out, you are type casting an octal number. What would you expect?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
eg:
Code: Select all
<?php echo (int) 0123; //outputs '83' due to 0123 being octal, not decimal. echo (int) 012; //outputs '10' ?>
On the other hand..
Code: Select all
echo (int) '0123'; // casting from string to integer gives 123I would expect exactly what it produced.. but I was pointing out that what others may look at as odd, is not odd at all.redmonkey wrote:This is expected behavoiur. As you point out, you are type casting an octal number. What would you expect?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
eg:
Code: Select all
<?php echo (int) 0123; //outputs '83' due to 0123 being octal, not decimal. echo (int) 012; //outputs '10' ?>
On the other hand..Code: Select all
echo (int) '0123'; // casting from string to integer gives 123
- blacksnday
- Forum Contributor
- Posts: 252
- Joined: Sat Jul 30, 2005 6:11 am
- Location: bfe Ohio :(
fixed typo in quote...it was killing me...redmonkey wrote:Code: Select all
<?php $var1 = '123.456'; $var2 = '123.'; echo ((int) $var1 == (int) $var2) ? 'true' : 'false'; ?>
thanks for that! thats awesome and actually
solves about 5 problems in one for me