mrras25 Posted October 22, 2007 Share Posted October 22, 2007 <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 More sharing options...
kratsg Posted October 23, 2007 Share Posted October 23, 2007 Well, another way would be the following: $num = "4.3"; $error_check = 0; foreach(range(4.1, 5.0, 0.1) as $check_num)){ if($check_num == $num){$error_check++;} } if($error_check){echo "No match found!";} else {echo "Match was found!";} Link to comment https://forums.phpfreaks.com/topic/74336-why-would-this-not-work/#findComment-375854 Share on other sites More sharing options...
Azu Posted October 23, 2007 Share Posted October 23, 2007 Or foreach(range(4.1, 5.0, 0.1)as $check_num))if($check_num=="4.3")$error_check=1; echo$error_check?'No match found!':'Match was found!'; Link to comment https://forums.phpfreaks.com/topic/74336-why-would-this-not-work/#findComment-376240 Share on other sites More sharing options...
BlueSkyIS Posted October 23, 2007 Share Posted October 23, 2007 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 https://forums.phpfreaks.com/topic/74336-why-would-this-not-work/#findComment-376248 Share on other sites More sharing options...
d.shankar Posted October 23, 2007 Share Posted October 23, 2007 I think range() wont work with float datatypes ??? Link to comment https://forums.phpfreaks.com/topic/74336-why-would-this-not-work/#findComment-376302 Share on other sites More sharing options...
roopurt18 Posted October 23, 2007 Share Posted October 23, 2007 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 https://forums.phpfreaks.com/topic/74336-why-would-this-not-work/#findComment-376348 Share on other sites More sharing options...
roopurt18 Posted October 23, 2007 Share Posted October 23, 2007 Playing with it a bit more, I see that any time I specify the third argument it returns null. (EDIT: Duh, it was introduced in 5.0 /slaps_forehead) Why not just write your own version? Link to comment https://forums.phpfreaks.com/topic/74336-why-would-this-not-work/#findComment-376353 Share on other sites More sharing options...
Azu Posted October 24, 2007 Share Posted October 24, 2007 Or update to PHP 5 PHP 4 has been deprecated for quite a while. As you can see there is good reason for this. Link to comment https://forums.phpfreaks.com/topic/74336-why-would-this-not-work/#findComment-376831 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.