Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best sintax for if($x=="a" ¦¦ $x=="b" ...)?? 1

Status
Not open for further replies.

rhowes

IS-IT--Management
Jun 6, 2001
140
ZA
Hi

I have many statements that look like:

if($x=="yes" || $x=="no" || $x="maybe")

Is ther a more efficient and elegant way of writing the condition?

Thanks.
Richard
 
You could make all the possible positive conditions into an array, with $x being the variable you are checking:
Code:
<?php

$conditions = Array(&quot;yes&quot;, &quot;no&quot;, &quot;maybe&quot;);

if(in_array($x, $conditions))
{
  //etc...
}
?>

This way, you can easily change your list of conditions, without having to change any other code. See PHP also has many other sophisticated array matching, sorting and comparison functions, so you can extend this approach in many ways.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top