Jump to content

How to write this


cmb

Recommended Posts

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/265734-how-to-write-this/#findComment-1361814
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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