similar both equal true?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

similar both equal true?

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

thanks.
thats what I was thinking, but wanting to confirm :)
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Code: Select all

<?php

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

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

?>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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' 

?>
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post 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
Post Reply