jaxdevil Posted July 2, 2008 Share Posted July 2, 2008 <?php if ($load_dock=='Load_Dock_Yes') { $str2 = $rate; list ($str2) = explode('.',ltrim($str2,'$')); $str=$str2*1.13; } ?> <?php if ($load_dock!='Load_Dock_Yes') { $str2 = ($rate+75); list ($str2) = explode('.',ltrim($str2,'$')); $str=$str2*1.13; } ?> What the did I do wrong here? Quote Link to comment https://forums.phpfreaks.com/topic/112917-problem-with-if-statement/ Share on other sites More sharing options...
mbeals Posted July 2, 2008 Share Posted July 2, 2008 how is it behaving now and how do you want it to behave? You are also doing some funky type conversion, which could be causing issues until we know what exactly is going on here, i'm not really sure where to begin Quote Link to comment https://forums.phpfreaks.com/topic/112917-problem-with-if-statement/#findComment-579997 Share on other sites More sharing options...
revraz Posted July 2, 2008 Share Posted July 2, 2008 Don't know, tell us what it is or isn't doing. Quote Link to comment https://forums.phpfreaks.com/topic/112917-problem-with-if-statement/#findComment-579998 Share on other sites More sharing options...
jaxdevil Posted July 2, 2008 Author Share Posted July 2, 2008 Its supposed to add $75 if the $load_dock value is not Load_Dock_Yes it adds 75 to the $rate value, if it does equal Load_Dock_Yes the $rate value remains unchanged. Quote Link to comment https://forums.phpfreaks.com/topic/112917-problem-with-if-statement/#findComment-580007 Share on other sites More sharing options...
revraz Posted July 2, 2008 Share Posted July 2, 2008 What if it doesn't equal either one? Quote Link to comment https://forums.phpfreaks.com/topic/112917-problem-with-if-statement/#findComment-580015 Share on other sites More sharing options...
jaxdevil Posted July 2, 2008 Author Share Posted July 2, 2008 It has to equal either one. If it is nothing then it will not equal $load_dock, so in any event, it will either equal that value or it will not equal it. Quote Link to comment https://forums.phpfreaks.com/topic/112917-problem-with-if-statement/#findComment-580018 Share on other sites More sharing options...
revraz Posted July 2, 2008 Share Posted July 2, 2008 <?php if ($load_dock == 'Load_Dock_Yes') { $str2 = $rate; list ($str2) = explode('.',ltrim($str2,'$')); $str=$str2*1.13; } else { $str2 = ($rate+75); list ($str2) = explode('.',ltrim($str2,'$')); $str=$str2*1.13; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/112917-problem-with-if-statement/#findComment-580022 Share on other sites More sharing options...
jaxdevil Posted July 2, 2008 Author Share Posted July 2, 2008 The problem is in this line here... $str2 = ($rate+75); Which I have tried as $str2 = $rate+75; and $str2 = '$rate'+'75'; Quote Link to comment https://forums.phpfreaks.com/topic/112917-problem-with-if-statement/#findComment-580023 Share on other sites More sharing options...
jaxdevil Posted July 2, 2008 Author Share Posted July 2, 2008 That code above is much more efficient than mine but the result is still the same. It is simply adding 75 to nothing if the results is not Load_Dock_Yes. In the else area it seems to be marking the $rate as nothing, not zero, just non-existent. Like it has it in the first part of the if statement and it losses it half way through. Quote Link to comment https://forums.phpfreaks.com/topic/112917-problem-with-if-statement/#findComment-580025 Share on other sites More sharing options...
mbeals Posted July 2, 2008 Share Posted July 2, 2008 i think your main issue is type conversion. $rate starts as a string, but you want to process it like a float. To convert it to a float, you started off correctly with the ltrim to ditch the '$'. Once that is gone, we need to convert $rate into a number. This is called type casting and in php it is done with (type): So the first part of the code is : $rate_num = (float)ltrim($rate,'$'); There I stripped the $ off the string, converted it to a float. Those steps were common to both if blocks, so I pulled them out. Now all we need to do is check the value of load_dock and add 75 and then multiply by 1.13. if ($load_dock=='Load_Dock_Yes') $rate_num += 75; $rate_num *= 1.13; ; So if $load_dock == 'Load_Dock_Yes' we add 75. If it doesn't, nothing happens. Then we finally multiply the number by 1.13. The += is short hand for $rate_num = $rate_num + 75. If you want to be technical, it is also a faster (computationally) way of doing it as well. Same for the *=. so the overall code is: <? $rate_num = (float)ltrim($rate,'$'); if ($load_dock=='Load_Dock_Yes') $rate_num += 75; $rate_num *= 1.13; ?> Quote Link to comment https://forums.phpfreaks.com/topic/112917-problem-with-if-statement/#findComment-580027 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.