Re: IF with multiple values for a variable
| From: | tg-php at gryffyndevelopment dot com | Date: | Wed, 13 Jun 2007 17:53:13 +0000 |
| Subject: | Re: IF with multiple values for a variable | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-256904@lists.php.net to get a copy of this message | ||
Could try something like this:
$types = array('T', 'D', 'L');
if (in_array($type, $types)) {
// do something
}
But that's going to just check to see if $type is one of the valid types you're looking
for.
You might try something like switch.
switch ($type) {
case 'T':
// Output 'T' record type
break;
case 'D':
// Output 'D' record type
break;
case 'L':
// Output 'L' record type
break;
default:
break;
}
"switch" works really well when you have multiple "if" scenarios and don't
want to keep doing a ton of if.. elseif... elseif...elseif... for simple comparisons.
You can even use it for more complicated comparisons:
switch (true) {
case $type == 'T':
break;
case $type == 'D' and $flavor <> 'grape':
break;
default;
break;
}
the "default" section is what's processed if no critera are met.
Also, if you want to use the same criteria for multiple conditions, or slight variations, you can do
that by removing 'break' codes:
switch ($type) {
case 'T':
case 'D':
// Do something if $type is either T or D
break;
case 'L':
// Do something L specific
case 'R':
// Do something if $type is L or R (passes through from L condition because break was removed)
break;
default:
break;
}
More reading here:
http://us.php.net/switch
Hope that helps.
-TG
= = = Original message = = =
Okay, I know this has got to be easy but it's not working any way I try it.
When a record is selected it opens up a new page. My query will display the
specific results based on the type of record selected. There can be
multiple values in each if. However I am having a brain fart in my if
statement assigning the multiple values.
Here is my if statement:
if ($type == 'T','D','L'
$get_id.=" payment_request WHERE id = '$payment_id'";
However this does not return ant results.
I've tried if ($type = array('T','D','L'), if ($type ==
array('T','D','L'),
if ($type == 'T,D,L'), if ($type == 'T' or 'D' or 'L'
I also looked in the PHP manual for examples of if's and didn't find any
help.
I can get it to work if I create a seperate if statement for each type
condition but i would prefer to not duplicate my code just to add a seperate
$type
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.