For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > May 2004 > shorter way to write a comparison??









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author shorter way to write a comparison??
Paweł

2004-05-26, 2:34 pm

Let's say I need to make a comparison

if ($var=="A" || $var=="B" || $var=="C" || ... and so on...

that would be something like:
if $var equals "A" or $var equals "B" or $var equals "C" .....
in natural language.

is there any way to state like:
if $var equals "A" or "B" or "C"????
shortbackandsides.no@spam.hairdresser.net

2004-05-26, 4:32 pm

How about using a switch statement - at its simplest:

switch ($var) {
case "A":
do something;
break;
case "B":
do something;
break;
case "C":
do something;
break;
default:
do something else;
}



On Wed, 26 May 2004 19:33:39 +0200, Paweł <pmg3@op.pl> wrote:

>Let's say I need to make a comparison
>
>if ($var=="A" || $var=="B" || $var=="C" || ... and so on...
>
>that would be something like:
>if $var equals "A" or $var equals "B" or $var equals "C" .....
>in natural language.
>
>is there any way to state like:
>if $var equals "A" or "B" or "C"????


Chris Hope

2004-05-26, 4:32 pm

shortbackandsides.no@spam.hairdresser.net wrote:

> How about using a switch statement - at its simplest:
>
> switch ($var) {
> case "A":
> do something;
> break;
> case "B":
> do something;
> break;
> case "C":
> do something;
> break;
> default:
> do something else;
> }


Although in the original poster's example it would work more like:

switch($var) {
case "A":
case "B":
case "C":
case "D":
do_something();
break;
default:
do_something_else();
}

--
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/
Shane Lahey

2004-05-26, 5:31 pm

On Wed, 26 May 2004 20:10:04 +0100,
shortbackandsides.no@spam.hairdresser.net wrote:

>How about using a switch statement - at its simplest:
>
>switch ($var) {
>case "A":
> do something;
> break;
>case "B":
> do something;
> break;
>case "C":
> do something;
> break;
>default:
> do something else;
>}
>


holy hell, it'd be much easier to do all the if()'s rather than
running it through switch():D

Savut

2004-05-28, 11:32 am

I prefer, not the fastest way, but the most easier to read.

$value = array("A","B","C");
if (in_array($var, $value)) {
//do this
}

Savut

"Paweł" <pmg3@op.pl> wrote in message
news:18oh76nyzfkqt$.1kwe5izofq5lq.dlg@40tude.net...
> Let's say I need to make a comparison
>
> if ($var=="A" || $var=="B" || $var=="C" || ... and so on...
>
> that would be something like:
> if $var equals "A" or $var equals "B" or $var equals "C" .....
> in natural language.
>
> is there any way to state like:
> if $var equals "A" or "B" or "C"????


Rook

2004-05-29, 5:31 am

//let's see how many different ways I can think of to skin this cat:

// the good 'ol `if` method
if (($var == 'A') OR ($var == 'B') OR ($var == 'C')) {
// do something
}

// you can use a switch statement (faster than using the `if` method)...
switch ($var) {
case 'A':
case 'B':
case 'C':
// your code here..
break;
default:
// default code here...
);

// array method #1
$this_arr = array('A','B','C');
if (in_array($var, $this_arr)) {
// do something
}

// array method #2
$this_arr = array('A'=>'Aye','B'=>'bee','C'=>'Si');
if (array_key_exists($var, $this_arr)) {
// do something
}

// hehe.. just being creative now...
if (in_array($var, range('A', 'C'))) {
// do something
}

// of course there is more... but that's all from me for now!

"Paweł" <pmg3@op.pl> wrote in message
news:18oh76nyzfkqt$.1kwe5izofq5lq.dlg@40tude.net...
> Let's say I need to make a comparison
>
> if ($var=="A" || $var=="B" || $var=="C" || ... and so on...
>
> that would be something like:
> if $var equals "A" or $var equals "B" or $var equals "C" .....
> in natural language.
>
> is there any way to state like:
> if $var equals "A" or "B" or "C"????



Shane Lahey

2004-05-30, 3:31 am

On Wed, 26 May 2004 19:33:39 +0200, Paweł <pmg3@op.pl> wrote:

>Let's say I need to make a comparison
>
>if ($var=="A" || $var=="B" || $var=="C" || ... and so on...
>
>that would be something like:
>if $var equals "A" or $var equals "B" or $var equals "C" .....
>in natural language.
>
>is there any way to state like:
>if $var equals "A" or "B" or "C"????


could make a function for it? :D


<?php

//usage checkvar($var, 'a', 'b', 'c', ...)
function checkvar()
{
$numargs = func_num_args();

$var_to_check = func_get_arg($i);

for ($i = 1; $i < $numargs; $i++)
{
$arg = func_get_arg($i);
if ($var_to_check == $arg)
return true; // match
}
return false; // no matches
}

?>

the above would be used something like this:
if (checkvar($var, 'A', 'B', 'C', 'D, 'E', 'F'))
echo "$var = A, B, C, D, E or F\n";
else
echo "$var IS NOT = A, B, C, D, E or F\n";

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com