Reply to Re: newbie alert: how to write this in a shorter way

Your name:

Reply:


Posted by hackajar on 12/26/06 00:57

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:
> 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:
> >> Michael Fesser wrote:
> >>
> >>> .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))) {
> >>> ...
> >>> }
> >> 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/
> >

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация