ETAN
ETAN

Reputation: 3182

comparing multiple data in php?

how can i compare multiple data using php

for example i have a variable $foo , the data assigned to it varies . if there are four different data can be assigned to variable $foo , like :" car , van,bus or bicycle"

how can i use the if statement to check which data is assigned to $foo?

normally if i have two data i will use

if($foo == 'firstdata'){
   //execute something as this is the first data
}
else{
   //this is the second data!
}

if there's 3 data i will use elseif statement.

but now 4 data , what can i use ? or can i use elseif as many times as i want??

Upvotes: 0

Views: 51

Answers (2)

Marc B
Marc B

Reputation: 360842

There's no practical limit to how many if/elseif/elseif/elseif you can chain together. Another alternative is to use a switch statement:

switch ($foo) {
   case 'firstdata':
      ... do something with firstdata ...
      break;
   case 'car':
      ... do car stuff
      break;
   case 'van':
      ... do van stuff
      break;
   ...
   default:
      ... do stuff with an unknown datum
}

Upvotes: 4

xdazz
xdazz

Reputation: 160933

Yes, you can use elseif as many times as you want, or you can use the switch statement .

Upvotes: 1

Related Questions