Home > Archive > PHP Language > December 2006 > newbie alert: how to write this in a shorter way
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 |
newbie alert: how to write this in a shorter way
|
|
|
| Hi all,
Sorry for the bad subject line, I wouldn't know how to describe this.
I'm sure this is common knowledge for anyone who knows PHP, i.e. not
me, but I can't find the answer online.
I'm just looking for a way to write the following in a shorter way:
if ($a == 1 || $a == 2 || $a == 3 || $a == 4 || $a == 17 || $a == 30)
Something that equates to "if $a == one of ((1 - 4), 17, 30)"
TIA,
--
Els http://locusmeus.com/
| |
| KoopaJah 2006-12-20, 3:58 am |
| Hello,
A simple possibility could be to declare an array with all the values
that $a cannot take and use the in_array() function. Something like this:
$forbiddenValues = array(1,2,3,4,17,30);
if (in_array($a, $forbiddenValues ))
{
// some code
}
But it does not allow you to define something like "all values between 1
and 4".
KooPa
Els wrote :
> Hi all,
>
> Sorry for the bad subject line, I wouldn't know how to describe this.
> I'm sure this is common knowledge for anyone who knows PHP, i.e. not
> me, but I can't find the answer online.
>
> I'm just looking for a way to write the following in a shorter way:
> if ($a == 1 || $a == 2 || $a == 3 || $a == 4 || $a == 17 || $a == 30)
>
> Something that equates to "if $a == one of ((1 - 4), 17, 30)"
>
> TIA,
>
| |
|
| KoopaJah wrote:
> Els wrote :
>
> A simple possibility could be to declare an array with all the values
> that $a cannot take and use the in_array() function. Something like this:
>
> $forbiddenValues = array(1,2,3,4,17,30);
> if (in_array($a, $forbiddenValues ))
> {
> // some code
> }
Thanks, works perfect. (albeit it's not about forbidden values, but
rather accepted values ;-) )
> But it does not allow you to define something like "all values between 1
> and 4".
That's a shame, but it's still better than what I had.
Thanks!
--
Els http://locusmeus.com/
| |
| Geoff Berrow 2006-12-20, 6:58 pm |
| Message-ID: <15270vgkskofw$.ex7bxqbswi4v.dlg@40tude.net> from Els
contained the following:
>I'm just looking for a way to write the following in a shorter way:
>if ($a == 1 || $a == 2 || $a == 3 || $a == 4 || $a == 17 || $a == 30)
>
>Something that equates to "if $a == one of ((1 - 4), 17, 30)"
I've just started playing round with regexp so this may not be optimal
$pattern = '/(?<![0-9])([1-4]|17|30)(?![0-9])/';
if(preg_match($pattern, $a)>0){
//do stuff
}
--
Geoff Berrow 0110001001101100010000000110
0011011010110110010001101111011001110010
11
1001100011011011110010111001110101011010
11
| |
|
| Geoff Berrow wrote:
> Message-ID: <15270vgkskofw$.ex7bxqbswi4v.dlg@40tude.net> from Els
> contained the following:
>
>
> I've just started playing round with regexp so this may not be optimal
>
> $pattern = '/(?<![0-9])([1-4]|17|30)(?![0-9])/';
> if(preg_match($pattern, $a)>0){
> //do stuff
> }
Well, I wouldn't use a regex for this, but if I did:
$pattern = '/^(?:[1-4]|17|30)$/';
We're not looking for a number in a string, but rather matching the entire
variable. And offcourse not capturing as we're not going to make use of
matches further on.
--
Rik Wasmus
| |
| Tim Streater 2006-12-20, 6:58 pm |
| In article <15270vgkskofw$.ex7bxqbswi4v.dlg@40tude.net>,
Els <els.aNOSPAM@tiscali.nl> wrote:
> Hi all,
>
> Sorry for the bad subject line, I wouldn't know how to describe this.
> I'm sure this is common knowledge for anyone who knows PHP, i.e. not
> me, but I can't find the answer online.
>
> I'm just looking for a way to write the following in a shorter way:
> if ($a == 1 || $a == 2 || $a == 3 || $a == 4 || $a == 17 || $a == 30)
>
> Something that equates to "if $a == one of ((1 - 4), 17, 30)"
I'd be inclined to leave it as it is, and let the interpreter optimise
it. That's its job, after all. As you've written it it's quite clear
what you intend.
-- tim
| |
|
| Tim Streater wrote:
> In article <15270vgkskofw$.ex7bxqbswi4v.dlg@40tude.net>,
> Els <els.aNOSPAM@tiscali.nl> wrote:
>
>
> I'd be inclined to leave it as it is, and let the interpreter optimise
> it. That's its job, after all. As you've written it it's quite clear
> what you intend.
It's just that I'm not worried about what the interpreter needs to do,
but what *I* need to do. (lazy typist me ;-))
--
Els http://locusmeus.com/
| |
| Michael Fesser 2006-12-20, 6:58 pm |
| ..oO(KoopaJah)
>A simple possibility could be to declare an array with all the values
>that $a cannot take and use the in_array() function. Something like this:
>
>$forbiddenValues = array(1,2,3,4,17,30);
>if (in_array($a, $forbiddenValues ))
>{
> // some code
>}
>
>But it does not allow you to define something like "all values between 1
>and 4".
if (in_array($a, range(1, 4))) {
...
}
Micha
| |
|
| On Wed, 20 Dec 2006 10:45:02 +0100, Els <els.aNOSPAM@tiscali.nl> wrote:
>Hi all,
>
>Sorry for the bad subject line, I wouldn't know how to describe this.
>I'm sure this is common knowledge for anyone who knows PHP, i.e. not
>me, but I can't find the answer online.
>
>I'm just looking for a way to write the following in a shorter way:
>if ($a == 1 || $a == 2 || $a == 3 || $a == 4 || $a == 17 || $a == 30)
>
>Something that equates to "if $a == one of ((1 - 4), 17, 30)"
>
>TIA,
In situation like this I like to use in_array function
$numbers = array(1, 2, 3, 4, 17, 30 );
if ( in_array($a , $numbers) ) {
echo "have a match";
}
| |
|
| Els schrieb:
> Hi all,
>
> Sorry for the bad subject line, I wouldn't know how to describe this.
> I'm sure this is common knowledge for anyone who knows PHP, i.e. not
> me, but I can't find the answer online.
>
> I'm just looking for a way to write the following in a shorter way:
> if ($a == 1 || $a == 2 || $a == 3 || $a == 4 || $a == 17 || $a == 30)
>
> Something that equates to "if $a == one of ((1 - 4), 17, 30)"
>
> TIA,
>
Just for the record:
$num = array(1=>1,2=>1,3=>1,10=>1);
$a=10;
if(isset($num[$a])){
echo "Ain't that ";
}
| |
|
| Michael Fesser wrote:
> .oO(KoopaJah)
>
>
> if (in_array($a, range(1, 4))) {
> ...
> }
Could I mix those two things, like so:
$values = array(range(1, 4),17,30);
if (in_array($a, $values))
{
echo "bingo";
}
?
--
Els http://locusmeus.com/
| |
| Michael Fesser 2006-12-21, 7:58 am |
| ..oO(Els)
>Could I mix those two things, like so:
>$values = array(range(1, 4),17,30);
That would make the array returned by range() a sub-array of $values,
which won't work as expected with in_array(). To solve that you could
* use array_merge() to merge both arrays together before calling
in_array()
* write a function in_array_recursive() to search in a nested array
structure if necessary
Micha
| |
|
| Michael Fesser wrote:
> .oO(Els)
>
>
> That would make the array returned by range() a sub-array of $values,
> which won't work as expected with in_array(). To solve that you could
>
> * use array_merge() to merge both arrays together before calling
> in_array()
> * write a function in_array_recursive() to search in a nested array
> structure if necessary
Sounds great, but as I am a total newbie, I think I'd go for the
array_merge() function though, it seems (from where I'm standing)
simpler than the recursive thing.
Would this be how it works?
$array1 = range(1,4);
$array2 = array(17,30);
$values = array_merge($array1,$array2);
if (in_array($a,$values){
echo "bingo";
}
PHP.net gave a caution about the range() function:
"In PHP versions 4.1.0 through 4.3.2, range() sees numeric strings as
strings and not integers. Instead, they will be used for character
sequences. For example, "4242" is treated as "4"."
Would that cause trouble if I'd have
$array = range(31,47);
?
--
Els http://locusmeus.com/
| |
|
| "Els" <els.aNOSPAM@tiscali.nl> wrote in message
news:1wya57gvvqad3.11ghuwvp7k4p7.dlg@40tude.net...
> Michael Fesser wrote:
>
>
> Sounds great, but as I am a total newbie, I think I'd go for the
> array_merge() function though, it seems (from where I'm standing)
> simpler than the recursive thing.
>
> Would this be how it works?
>
> $array1 = range(1,4);
> $array2 = array(17,30);
> $values = array_merge($array1,$array2);
> if (in_array($a,$values){
> echo "bingo";
> }
Exactly. You are missing a ) though.
> PHP.net gave a caution about the range() function:
> "In PHP versions 4.1.0 through 4.3.2, range() sees numeric strings as
> strings and not integers. Instead, they will be used for character
> sequences. For example, "4242" is treated as "4"."
>
> Would that cause trouble if I'd have
> $array = range(31,47);
> ?
That would cause problems only if you passed strings to range() instead of integers.
-Lost
| |
|
| -Lost wrote:
> Exactly. You are missing a ) though.
Yup - noted, thanks.
>
> That would cause problems only if you passed strings to range() instead of integers.
As in range('31','47')?
What defines a string - the quotes?
--
Els http://locusmeus.com/
| |
|
| "Els" <els.aNOSPAM@tiscali.nl> wrote in message
news:ydcvw9wiyfpf.12113dok9338b.dlg@40tude.net...
> -Lost wrote:
>
>
> Yup - noted, thanks.
>
>
> As in range('31','47')?
> What defines a string - the quotes?
Yep.
-Lost
| |
|
| -Lost wrote:
> "Els" <els.aNOSPAM@tiscali.nl> wrote in message
> news:ydcvw9wiyfpf.12113dok9338b.dlg@40tude.net...
>
> Yep.
Thanks :-)
--
Els http://locusmeus.com/
| |
| hackajar@gmail.com 2006-12-25, 7:58 am |
| If you really want it to look like a range:
<?php
$a=17;
if($a >= 1 || $a <= 4 || $a == 17 || $a ==30) echo "Bingo";
?>
Hackajar
Els wrote:
> Michael Fesser wrote:
>
>
> Could I mix those two things, like so:
> $values = array(range(1, 4),17,30);
> if (in_array($a, $values))
> {
> echo "bingo";
> }
>
> ?
>
> --
> Els http://locusmeus.com/
| |
|
| hackajar@gmail.com schrieb:
> If you really want it to look like a range:
>
> <?php
> $a=17;
>
> if($a >= 1 || $a <= 4 || $a == 17 || $a ==30) echo "Bingo";
> ?>
Dude that's major wrong your "if" is true for any numbers, because first:
$a >= 1
will always be true if you have a positiv number above 1. Second:
$a <= 4
any negative number including the 1 and 0 that you didn't catch with the
first rule.
The condition
$a == 17 || $a ==30
will never be reached, since they are already covered by:
$a >= 1
I think what you meant is:
if(($a >= 1 && $a <= 4) || $a == 17 || $a ==30)
right:-) Very important the brackets for condition "$a >= 1 && $a <= 4"
>
> Hackajar
> Els wrote:
>
| |
| hackajar@gmail.com 2006-12-25, 6:58 pm |
| I was thinking about that when I woke up this morning ;) Good catch!
Maybe this would be more creative (not to complicate this even more;):
<?php
function checkA($a) {
switch($a) {
case 1:return true; break;
case 2:return true; break;
case 3:return true; break;
case 4:return true; break;
case 17:return true; break;
case 30:return true; break;
}
return false;
}
if(checkA($a))echo "bingo!";
?>
-Hackajar
Ric wrote:[color=darkred]
> hackajar@gmail.com schrieb:
>
> Dude that's major wrong your "if" is true for any numbers, because first:
>
> $a >= 1
>
> will always be true if you have a positiv number above 1. Second:
>
> $a <= 4
>
> any negative number including the 1 and 0 that you didn't catch with the
> first rule.
>
> The condition
>
> $a == 17 || $a ==30
>
>
> will never be reached, since they are already covered by:
>
> $a >= 1
>
> I think what you meant is:
>
> if(($a >= 1 && $a <= 4) || $a == 17 || $a ==30)
>
> right:-) Very important the brackets for condition "$a >= 1 && $a <= 4"
>
| |
| Ken Robinson 2006-12-25, 9:59 pm |
| hackajar@gmail.com wrote:
> I was thinking about that when I woke up this morning ;) Good catch!
>
> Maybe this would be more creative (not to complicate this even more;):
> <?php
>
> function checkA($a) {
> switch($a) {
> case 1:return true; break;
> case 2:return true; break;
> case 3:return true; break;
> case 4:return true; break;
> case 17:return true; break;
> case 30:return true; break;
> }
> return false;
> }
>
> if(checkA($a))echo "bingo!";
> ?>
If you're going to make this into a switch statement, group the "true"
values together:
<?php
function checkA($a) {
$retVal = false;
switch($a) {
case 1:
case 2:
case 3:
case 4:
case 17:
case 30:
$retVal = true;
break;
}
return ($retVal);
}
if (checkA($a)) echo 'Ok!';
?>
Ken
|
|
|
|
|