jacko_162 Posted March 10, 2010 Share Posted March 10, 2010 i have a set of values that i echo on a page; $test1, $test2, $test3 etc etc.. i want an if statement to show if the said value is between X and y show blueicon.png else show redicon.png i have been googling syntax but to no avail. this is my current code snippet; <?php echo $test1 ?> <? if ($test1 < 1.025) echo "show blue icon.png"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/194777-if-data-is-between-then-echo/ Share on other sites More sharing options...
Wolphie Posted March 10, 2010 Share Posted March 10, 2010 <?php $val1 = 5; $val2 = 10; $val3 = 7; if (($val3 > $val1) && ($val3 < $val2)) { echo 'icon.png'; } else { echo 'othericon.png'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/194777-if-data-is-between-then-echo/#findComment-1024206 Share on other sites More sharing options...
Adam Posted March 10, 2010 Share Posted March 10, 2010 You could use something along the lines of: if ($test1 > $x && $test1 < $y) { echo '<img src="blueicon.jpg" />'; } else { echo '<img src="redicon.jpg" />'; } However for something like this you may be better off using the ternary operator (scroll down to example 2): <img src="<?php echo ($test1 > $x && $test1 < $y) ? 'blueicon.jpg' : 'redicon.jpg'; ?>" /> Edit: not sure given your original description and Wolphie's reply, which numbers you want to compare.. Quote Link to comment https://forums.phpfreaks.com/topic/194777-if-data-is-between-then-echo/#findComment-1024208 Share on other sites More sharing options...
Wolphie Posted March 10, 2010 Share Posted March 10, 2010 Me either really, I just took a shot in the dark. Quote Link to comment https://forums.phpfreaks.com/topic/194777-if-data-is-between-then-echo/#findComment-1024210 Share on other sites More sharing options...
jacko_162 Posted March 10, 2010 Author Share Posted March 10, 2010 Me either really, I just took a shot in the dark. each statement will have the numbers entered manually. for instance; IF $test1 = is between 1.020 & 1.027 = show blueicon.png else show redicon.png hope this clears it up, tbh i could have these set values in the database but for now i need them in the code. Quote Link to comment https://forums.phpfreaks.com/topic/194777-if-data-is-between-then-echo/#findComment-1024213 Share on other sites More sharing options...
Adam Posted March 10, 2010 Share Posted March 10, 2010 Replacing the $x and $y vars in my example should be what you need. Quote Link to comment https://forums.phpfreaks.com/topic/194777-if-data-is-between-then-echo/#findComment-1024219 Share on other sites More sharing options...
jacko_162 Posted March 10, 2010 Author Share Posted March 10, 2010 cheers guys Wolphie version seemed the better option and its the first i tried problem now solved. Quote Link to comment https://forums.phpfreaks.com/topic/194777-if-data-is-between-then-echo/#findComment-1024236 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.