cmb Posted July 16, 2012 Share Posted July 16, 2012 what is the correct way to wright this while ( 5 < $c <= 9){ } Quote Link to comment https://forums.phpfreaks.com/topic/265734-how-to-write-this/ Share on other sites More sharing options...
cmb Posted July 16, 2012 Author Share Posted July 16, 2012 while ( 5 < $c && $c <= 9){ } Quote Link to comment https://forums.phpfreaks.com/topic/265734-how-to-write-this/#findComment-1361794 Share on other sites More sharing options...
Donald. Posted July 16, 2012 Share Posted July 16, 2012 That should work fine. <?php $c = 6; while ( 5 < $c && $c <= 9){ //Do something } ?> What's the problem? Quote Link to comment https://forums.phpfreaks.com/topic/265734-how-to-write-this/#findComment-1361797 Share on other sites More sharing options...
carugala Posted July 16, 2012 Share Posted July 16, 2012 What's the problem? me too! there is no 'correct way'. either the logic works, or it doesn't. Looks to me, you want $c to be gt 5 and lte 9 to continue the loop.... coupla things, style wise: make compound conditionals consistent. if (cond1 && cond2 && cond3), condX should be consistent. 5<$var and $var>5 are logically the same, so cond1, cond2, cond3 should be the same format... this is piddling, but good practice, hence: while ( 5 < $c && $c <= 9) is better written as while ($c>5 && $c<=9)... this is piddlin, along the same lines, but perhaps more useful. with conditional loops, make the condition ASAP, as in KISS. Actually works well for all conditionals... I want to find all female canucks of legal age, who can driveget me to the border. if ($sex == f && $nationality = can && $age>21 && issset($driver_id) && $hasGPS) logically may work, but is horrible to figure out, throw in some () and/or, and gets messy fast.... whereas: while($notFound) OR while($found) OR while(~$found) is lightly more elegant in a human way, or not. just passing logic to var value, syntactically makes conditional loops easier to deal with. oops, imho $foobar = //var to test $good = new array(6,7,8,9); while(in_array($foobar, $good)) Quote Link to comment https://forums.phpfreaks.com/topic/265734-how-to-write-this/#findComment-1361814 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.