Streamlining Code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Deddog
Forum Commoner
Posts: 55
Joined: Thu Sep 26, 2002 6:05 am
Location: Brighton

Streamlining Code

Post 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.
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post 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. :)
User avatar
DaZZleD
Forum Commoner
Posts: 38
Joined: Tue Jan 07, 2003 5:39 am

Post 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;
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post 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.
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post 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:
User avatar
DaZZleD
Forum Commoner
Posts: 38
Joined: Tue Jan 07, 2003 5:39 am

Post by DaZZleD »

very funny! :evil:
Post Reply