Jump to content

Why would this NOT work?


mrras25

Recommended Posts

<code>

$num = "4.3";

 

$style = range(4.1, 5.0, 0.1);

 

if(in_array($num, $style)) {

  echo "Found in Array!!!\n";

} else {

  echo "Not SO MUCH \n";

}

</code>

 

And how would I get this to work? In theory it should be in that array correct? I have broken it down and it is in there however when I run this I receive the "else" statement.

 

I am actually trying to set style based on a number range

 

"if(range(0.0, 1.0, 0.1) == $num){ set style to this } elseif(range(1.1, 2.0, 0.1) == $num) { set style to this} elseif(...........) (so on so forth up to 5.0)

If someone has a better way to do this please let me know, I am at a lose right now.

Link to comment
https://forums.phpfreaks.com/topic/74336-why-would-this-not-work/
Share on other sites

i'm baffled as to why in_array() isn't working as described in the first post... we see the value in the array after it is created, but in_array() says it isn't in there

 

<?
$num = "4.3";

$style = range(4.1, 5.0, 0.1);

print_r($style); // We see 4.3 in the array $style

if(in_array($num, $style)) {
echo "$num Found in Array!!!\n";
} else {
echo "$num Not SO MUCH \n"; // But according to in_array, $num is not in the array $style
}
?>

 

strange...

Perhaps it doesn't work with floats?  Using PHP 4.4.4:

 

<?php
header('Content-type: text/plain');
$num = "4.3";

echo "hi\n";
$style = range(4.1, 5.0, 0.1);
print_r($style); // We see 4.3 in the array $style
echo $style === null ? "null\n" : "not null\n";
echo "bye\n";


if(in_array($num, $style)) {
echo "$num Found in Array!!!\n";
} else {
echo "$num Not SO MUCH \n"; // But according to in_array, $num is not in the array $style
}
?>

 

Produces

hi
null
bye
4.3 Not SO MUCH 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.