|
List Info
Thread: Simplifying a basic if statement conditional
|
|
| Simplifying a basic if statement
conditional |
  United States |
2007-05-19 06:06:12 |
Is there a more efficient way to write this:
if($var=1 || $var=4 || $var=27 || $var=28 || $var=30 ||
$var=37 ||
$var=38){echo "true";}
Cheers,
Ciarán
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |

|
2007-05-19 11:02:05 |
|
For it to work correctly, you at least need one more equal sign for each OR.
On 5/19/07, Ciaran < cronoklee hotmail.com">cronoklee hotmail.com
> wrote: Is there a more efficient way to write this: if($var=1 || $var=4 || $var=27 || $var=28 || $var=30 || $var=37 ||
$var=38){echo "true";}
Cheers, Ciarán
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP -~----------~----~----~----~------~----~------~--~---
|
| Re: Simplifying a basic if statement
conditional |

|
2007-05-19 11:39:47 |
|
You *could* do
if(in_array($var, array(1, 4, 27, 28, 30, 37, 38))echo "True";
But if you do it like this:
// In the main program
if(is_valid_week($week_number)) echo "True";
// In an included file holding only settings
function is_valid_week($week_number)
{
$valid_weeks = array(1, 4, 27, 28, 30, 37, 38);
return in_array($week_number, $valid_weeks);
}
then not only can you use the function elsewhere, but it is easier to understand what is happening since it's practically self-documenting.
If you had to maintain a program written by someone else, which would you prefer to see? After 6 months, every program seems like someone else wrote it when you come back to it
Ian
On 19/05/07, George Rodriguez < futbolguy gmail.com">futbolguy gmail.com> wrote:
For it to work correctly, you at least need one more equal sign for each OR.
-- Ian http://roughian.com
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP -~----------~----~----~----~------~----~------~--~---
|
| Re: Simplifying a basic if statement
conditional |
  United States |
2007-05-21 01:28:04 |
Why don't you use switch. use like this
switch($var){
case 1 :
case 4 :
case 27:
case 30:
case 37:
echo "true";break;
}
On May 19, 4:06 pm, Ciaran <cronok... hotmail.com> wrote:
> Is there a more efficient way to write this:
> if($var=1 || $var=4 || $var=27 || $var=28 || $var=30 ||
$var=37 ||
> $var=38){echo "true";}
>
> Cheers,
> Ciarán
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |
  United States |
2007-05-21 16:48:37 |
Hy,
Ravi, this is not the case, the initial topic has thrown
up.
He needs to check that a variable is equals one of a couple
of values,
and he wants to do the same thing in each case. So switch is
not the
right decision in this situation.
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |
  United States |
2007-05-21 17:37:37 |
What about this?
<?PHP
$varlist = array(1, 4, 27, 28, 30, 37, 38);
foreach($varlist as $key=>$val)
{
if($var == $val)
{
echo "True";
}
}
?>
On May 21, 2:48 pm, PreZ <prezdes... gmail.com> wrote:
> Hy,
>
> Ravi, this is not the case, the initial topic has
thrown up.
> He needs to check that a variable is equals one of a
couple of values,
> and he wants to do the same thing in each case. So
switch is not the
> right decision in this situation.
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |
  United States |
2007-05-22 08:56:33 |
ciaran
if and switch are both most efficient for the task, but
switch is more
elegant. if you use arrays and function calls just to check
things
this will be a) much slower for interpreter b) less clear
for
programmer c) less productive overall
prez
the switch in the mentionned form is an absolutely legal
replacement
for if
--
free web developer tools (now with sitemap support):
http://osbtools.goog
lepages.com/
On May 19, 1:06 pm, Ciaran <cronok... hotmail.com> wrote:
> Is there a more efficient way to write this:
> if($var=1 || $var=4 || $var=27 || $var=28 || $var=30 ||
$var=37 ||
> $var=38){echo "true";}
>
> Cheers,
> Ciarán
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |

|
2007-05-22 09:41:28 |
|
Unless you are in a tight loop, the array solution will have no discernable effect on speed maybe 100 flops at however many gigahertz you are running
If you are writing any non-trivial program, then you will need to have some kind of structuring to the code or it will rapidly become unmaintainable
Most time spent on an average commercial system during its lifespan (both design and coding) is actually in maintenance, and if you have ever tried to maintain a commercial system, you will know that well-structured, self-commenting code is far more productive overall that the deeply nested sequential kind of program you will end up with if you have everything in-line. And, of course, designing for code re-use is more productive than designing against it
Ian
On 22/05/07, stas < stas.trefilov gmail.com">stas.trefilov gmail.com> wrote:
ciaran
if and switch are both most efficient for the task, but switch is more elegant. if you use arrays and function calls just to check things
this will be a) much slower for interpreter b) less clear for programmer c) less productive overall
prez
the switch in the mentionned form is an absolutely legal replacement for if
-- free web developer tools (now with sitemap support):
http://osbtools.googlepages.com/
On May 19, 1:06 pm, Ciaran < cronok... hotmail.com">cronok... hotmail.com> wrote: > Is there a more efficient way to write this:
> if($var=1 || $var=4 || $var=27 || $var=28 || $var=30 || $var=37 || > $var=38){echo "true";} > > Cheers, > Ciarán
-- Ian http://roughian.com
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP -~----------~----~----~----~------~----~------~--~---
|
| Re: Simplifying a basic if statement
conditional |

|
2007-05-22 10:08:51 |
i appreciate your opinion but insist that using array +
function call
to check values of a simple variable is bad practice since
is
irrational solution and greatly increases wtf factor thus
complicating
the maintenance. it is of no use to reinvent the wheel for
the
mechanisms already designed for this particular problem,
switch
statement in our case. imagine people (or even yourself)
staring at
your code in a couple of months asking themselves a
question: what is
the purpose of this one more function in the code? if you
used the
switch you may be sure that you don't need to jump to the
function
library only to find out that the function call is a simple
wrapper
around a in_array () function...
don't add more abstractions where you may use native
language mechanisms
p.s. funny, i may use your own language to prove the
opposite
--
free web developer tools (now with sitemap support):
http://osbtools.goog
lepages.com/
On 5/22/07, Ian Bambury <ianbambury gmail.com> wrote:
> Unless you are in a tight loop, the array solution will
have no discernable
> effect on speed maybe 100 flops at however many
gigahertz you are running
>
> If you are writing any non-trivial program, then you
will need to have some
> kind of structuring to the code or it will rapidly
become unmaintainable
>
> Most time spent on an average commercial system during
its lifespan (both
> design and coding) is actually in maintenance, and if
you have ever tried to
> maintain a commercial system, you will know that
well-structured,
> self-commenting code is far more productive overall
that the deeply nested
> sequential kind of program you will end up with if you
have everything
> in-line. And, of course, designing for code re-use is
more productive than
> designing against it
>
> Ian
>
>
> On 22/05/07, stas <stas.trefilov gmail.com> wrote:
> >
> > ciaran
> >
> > if and switch are both most efficient for the
task, but switch is more
> > elegant. if you use arrays and function calls just
to check things
> > this will be a) much slower for interpreter b)
less clear for
> > programmer c) less productive overall
> >
> > prez
> >
> > the switch in the mentionned form is an absolutely
legal replacement
> > for if
> >
> > --
> > free web developer tools (now with sitemap
support):
> > http://osbtools.goog
lepages.com/
> >
> >
> > On May 19, 1:06 pm, Ciaran <cronok... hotmail.com> wrote:
> > > Is there a more efficient way to write this:
> > > if($var=1 || $var=4 || $var=27 || $var=28 ||
$var=30 || $var=37 ||
> > > $var=38){echo "true";}
> > >
> > > Cheers,
> > > Ciarán
> >
> >
> >
>
>
>
> --
> Ian
> http://roughian.com
> >
>
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |

|
2007-05-22 14:17:55 |
|
I hope I'm always willing to learrn but I find it hard to see how
switch($var){ case 1 : case 4 : case 27: case 30: case 37: echo "true";break;
is more readable than
if(is_valid_week($week_number)) echo "True";
the first tells you nothing about what things are happening, the second is shorter and IMNSHO more meaningful.
But let's take it a little further. Say these numbers are the product categories of zero-rated products for tax reasons. And say we have a typically-sized PHP stock order system of 30,000 lines of code in 150 php files, and just for arguments sake, let's say there are 10 places where you need to know if some product is zero-rated, would you have this snippet of code with its magic numbers in 10 places?
If not, what would you do?
And if you *did* have it in 10 places, and another product category became zero-rated, which would you rather do: search 30,000 lines of code in 150 files, or look in the "system_settings.php" file for the "is_zero_rated_product()" routine and add one number to an array?
And what if you had 10 systems? A third of a million lines of code, and how do you know that
switch($var){ case 1 : case 4 : case 27: case 30: case 37: echo "true";break;
doesn9;t refer to something else?
How would you approach the situation?
Ian
On 22/05/07, Stas Trefilov < stas.trefilov gmail.com">stas.trefilov gmail.com> wrote:
i appreciate your opinion but insist that using array + function call to check values of a simple variable is bad practice since is
irrational solution and greatly increases wtf factor thus complicating the maintenance. it is of no use to reinvent the wheel for the mechanisms already designed for this particular problem, switch statement in our case. imagine people (or even yourself) staring at
your code in a couple of months asking themselves a question: what is the purpose of this one more function in the code? if you used the switch you may be sure that you don't need to jump to the function
library only to find out that the function call is a simple wrapper around a in_array () function...
don't add more abstractions where you may use native language mechanisms
p.s. funny, i may use your own language to prove the opposite
-- free web developer tools (now with sitemap support): http://osbtools.googlepages.com/
On 5/22/07, Ian Bambury < ianbambury gmail.com">
ianbambury gmail.com> wrote: > Unless you are in a tight loop, the array solution will have no discernable > effect on speed maybe 100 flops at however many gigahertz you are running > > If you are writing any non-trivial program, then you will need to have some
> kind of structuring to the code or it will rapidly become unmaintainable > > Most time spent on an average commercial system during its lifespan (both > design and coding) is actually in maintenance, and if you have ever tried to
> maintain a commercial system, you will know that well-structured, > self-commenting code is far more productive overall that the deeply nested > sequential kind of program you will end up with if you have everything
> in-line. And, of course, designing for code re-use is more productive than > designing against it > > Ian > > > On 22/05/07, stas < stas.trefilov gmail.com">stas.trefilov gmail.com
> wrote: > > > > ciaran > > > > if and switch are both most efficient for the task, but switch is more > > elegant. if you use arrays and function calls just to check things
> > this will be a) much slower for interpreter b) less clear for > > programmer c) less productive overall > > > > prez > > > > the switch in the mentionned form is an absolutely legal replacement
> > for if > > > > -- > > free web developer tools (now with sitemap support): > > http://osbtools.googlepages.com/ > >
> > > > On May 19, 1:06 pm, Ciaran < cronok... hotmail.com">cronok... hotmail.com> wrote: > > > Is there a more efficient way to write this: > > > if($var=1 || $var=4 || $var=27 || $var=28 || $var=30 || $var=37 ||
> > > $var=38){echo "true";} > > > > > > Cheers, > > > Ciarán > > > > > > > > > > -- > Ian >
http://roughian.com > > >
-- Ian http://roughian.com
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP -~----------~----~----~----~------~----~------~--~---
|
|
|