Page 1 of 1

Streamlining Code

Posted: Tue Jan 07, 2003 5:45 am
by Deddog
if ($row["SpeedDial"] === 'Spd' or $row["SpeedDial"] === 'spd' or $row["SpeedDial"] == 'n/a' or $row["SpeedDial"]==='') :

How can i change the above code to be more efficient?

Can't find commands like "in".

If $row["SpeedDial"] in ('spd','Spd', ect ect

Cheers,

Lee.

Posted: Tue Jan 07, 2003 5:58 am
by Elmseeker

Code: Select all

if ($row["SpeedDial"] === ('Spd' || 'spd' || 'n/a' || ''));
That should turn the trick...not positive though, never tried it in PHP, works in C/C++ though. :)

Posted: Tue Jan 07, 2003 6:05 am
by DaZZleD
you could just define an array with all the possible values $row["SpeedDial"] can have. then use a for to make the comparisons:

Code: Select all

$possible_values = array("Spd", "spd", "n/a"...);
$found = 0;

for($x=0; $x<count($possible_values); $x++)
&#123;
  if($row&#1111;"SpeedDial"] === $possible_values&#1111;$x])
  &#123;
     $found = $possible_values&#1111;$x];
  &#125;
&#125;

if($found == 0)
&#123;
  //$row&#1111;"SpeedDial"] didn't have any of the values in $possible_values
&#125;
else
&#123;
  //$row&#1111;"SpeedDial"] had one of the values in $possible_values ($found)
&#125;

Posted: Tue Jan 07, 2003 6:18 am
by EvilWalrus
Elmseekers function would only work if the values were integers, not strings, and the 'in' command is only native to Perl. Use the suggested array solution to make it a little nicer.

Posted: Tue Jan 07, 2003 6:21 am
by Elmseeker
That is very good to know, thanks for the info Oh Evil One!

And Dazzled,
i did all this single-handed
Very sorry to hear about your loss, ever thought of getting a prosthetic hand? :twisted:

Posted: Tue Jan 07, 2003 6:31 am
by DaZZleD
very funny! :evil: