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
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...

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.