|
-
June 19th, 2008, 10:48 AM
#1
Bug in function, in_array()??
So...
This is user input:
PHP Code:
$range = array(2.55, 59.69);
This is parsed code:
PHP Code:
$okRange = array('min_range', 'max_range'); foreach($range as $k => $v) { if( in_array($k, $okRange) === false ) { echo "$k is invalid range flag!" . NL; } }
Response is:
HTML Code:
1 is invalid range flag!
But it was supposed to fail at: 0 is invalid range flag!
Why did int(0), passed through?!?
After I added a third param bool(true), to in_array(), then it worked like:
PHP Code:
... if( in_array($k, $okRange, true) === false ) ...
Bit it still doesn't make sense, why did int(0), passed through without third param bool(true)?!?
int(0), was compared to values 2.55 and 59.69.
So it was supposed to fail, like int(1) does!
Last edited by Ipsens; June 19th, 2008 at 10:54 AM.
Ipsens
-
June 19th, 2008, 03:21 PM
#2
Re: Bug in function, in_array()??
No bug here.
All you are doing is testing the index of the array $range. But, you are using the index, which is an integer, and comparing it with an array of strings. Strings will act as zero when parsed by a loosely typed language such as PHP.
Example:
PHP Code:
<?php
$okRange = array('min_range', 'max_range'); // found because string parses as zero
$okRange = array(8, 9); // not found in array
$okRange = array(0, 5, 9); // found because zero is present
$range = array(2.55, 59.69); // these values don't matter because
// you are comparing keys not the values
foreach ($range as $k => $v) {
if (in_array($k, $okRange)) { // check to see if $k is in the array $okRange
echo $k . ' found in array!<br />';
}
}
?>
P.S. You don't need to include the true because if automatically checks boolean.
Last edited by PeejAvery; June 19th, 2008 at 03:24 PM.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
June 19th, 2008, 03:44 PM
#3
Re: Bug in function, in_array()??
 Originally Posted by PeejAvery
No bug here.
All you are doing is testing the index of the array $range. But, you are using the index, which is an integer, and comparing it with an array of strings. Strings will act as zero when parsed by a loosely typed language such as PHP.
Yes, I see now, after having a little of sleep.
This is an exact comparision, that PHP does:
PHP Code:
if(0 == 'hgasj') { echo 'I see it, as a same! :P'; }
 Originally Posted by PeejAvery
P.S. You don't need to include the true because if automatically checks boolean.
I know that, but because PHP is loosely typed language, I am extremely defensive when coding and will alway narrow possible outcomes, as much as possible.
Like using instead etc...
Thanks!
Ipsens
-
June 19th, 2008, 04:50 PM
#4
Re: Bug in function, in_array()??
 Originally Posted by Ipsens
I know that, but because PHP is loosely typed language, I am extremely defensive when coding and will alway narrow possible outcomes, as much as possible.
Yes, but I was referring to your if statement itself.
You have...
PHP Code:
if(in_array($k, $okRange) === false){
But, it is much simpler and efficient to do...
PHP Code:
if(!in_array($k, $okRange)){
Or...for true...
PHP Code:
if(in_array($k, $okRange)){
Rather than adding extra waste...
PHP Code:
if(in_array($k, $okRange) === true){
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
June 20th, 2008, 03:24 AM
#5
Re: Bug in function, in_array()??
 Originally Posted by PeejAvery
Yes, but I was referring to your if statement itself.
You have...
PHP Code:
if(in_array($k, $okRange) === false){
Yes, I know you were reffering, to that
 Originally Posted by PeejAvery
But, it is much simpler and efficient to do...
PHP Code:
if(!in_array($k, $okRange)){
Or...for true...
PHP Code:
if(in_array($k, $okRange)){
That is exactly, how I did code, just a month ago, but after having a case where amount 0, evalueted to false, just because PHP's function by default used == instead === for comparasion of values, it made me snap and start using this defensive approach
 Originally Posted by PeejAvery
Rather than adding extra waste...
PHP Code:
if(in_array($k, $okRange) === true){
Yes, I know that is a true. For this case best would be:
PHP Code:
if( in_array($k, $okRange, true) ){
As it is max narrowed outcome.
Lastly, evaluation is far faster, when used === instead of ==
True of false? 
Ipsens
-
June 20th, 2008, 07:15 AM
#6
Re: Bug in function, in_array()??
 Originally Posted by Ipsens
Lastly, evaluation is far faster, when used === instead of ==
True of false?
It's not really an issue of speed. It all depends on if you want to check variable type as well.
Sometimes === will be faster, and sometimes == will be faster. It all depends on the situation. For example, if the variable type is different, === will be faster. But, if you know the variable type is the same, === still runs that extra check wasting more time.
Either way, the speed difference will be so small just because of this comparison operator, that you wouldn't even be able to tell the difference.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|