Re: [PHP] Re: Switch - Case Statement Questions

2012-11-19 Thread Andrew Ballard
On Sat, Nov 17, 2012 at 4:13 PM, Sebastian Krebs  wrote:
> 2012/11/17 Andrew Ballard 
>
>> On Nov 16, 2012 10:24 PM, "tamouse mailing lists" 
>> wrote:
>> >
>> > On Fri, Nov 16, 2012 at 12:41 PM, Sebastian Krebs 
>> wrote:
>> > > Beside this it can be rewritten as
>> > >
>> > > switch ((int) (($count-1) / 7) {
>> > >   case 0: // 1-7
>> > >   case 1: // 8 - 14
>> > >   default: // above 15
>> > > }
>> >
>> > Nice code refactoring :) Just a tad obscure for someone coming along
>> > later
>>
>> Not only obscure, but depending on the rule being processed could be plain
>> wrong. It works for the values shown so far, but what if another test is
>> added to the use case in the future that doesn't fit the clever solution?
>>
> Like so often in our developers world many things breaks, when it comes to
> new requirements.
>
>> Without knowing the intent of the code, it could be a headache to maintain.
>>
> Interesting, that you see 5 lines of code and assume, that nobody will ever
> get the intent of this code ;) Of course the context is missing. I guess,
> that "$count" is something like "remaining days", or such, because "7" and
> "14" look like "one week" and "two weeks", respectively. Thus I wouldn't
> name the variable "$count" [1], but "$remainingDays" and voila: Context is
> back and so is the intent :)
>
> Of course the above code has a quite limited use-case, but that was the
> requirement (for now, see KISS). If you need more, refactor/rewrite it.


That's precisely my point. If the solution you posted, complete with
your assumptions about the significance of the values "7" and "14",
matches the requirements then use it. If your assumptions were wrong,
and a future revision may potentially require special handling for
certain values like "3" or "8" that cannot be evaluated using your
equation, then the whole block may have to be refactored rather than
simply adding a couple lines for the case. In the spirit of "keeping
it simple," I would prefer the code that requires less refactoring.

I'm not assuming that "nobody will ever get the [coder's] intent" of a
segment of code at all. It's not so much about what the code does, as
WHY it is doing it.

> [1] Even further: I was taught, that if I ever want to name a variable like
> "count", "status", "type", ... I should stop and probably don't use this
> name. In most cases they are used wrong. This example here is quite good:
> "$count" what?

It depends. Generally I agree with you, but if it is a simple loop
counter in a small block of code where variable scope issues and
overall readability of the code is not diminished I'm fine with names
like $count or $n.

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Switch - Case Statement Questions

2012-11-19 Thread Sebastian Krebs
2012/11/19 Tim Streater 

> On 18 Nov 2012 at 14:44, Jim Giner  wrote:
>
> > 2. Using Switch {ALWAYS FIRST CASE!!!}
> >
> > //$boxes = 1;
> > //switch ($count) {
> > //case ($count > 14):
> > //$boxes = 3;
> > //break;
> > //case ($count > 7 && $count <= 14):
> > //$boxes = 2;
> > //break;
> > //case ($count <= 7):
> > //default :
> > //$boxes = 1;
> > //break;
> > //}
>
>
> > To answer Iñigo's comment - the OP's version is very much like an
> > If-ElseIF structure, and nothing like a Switch.
>
> Just so. Perhaps those who are not grasping the point could re-read their
> copy of "The Elements of Programming Style" by Kernighan and Plauger where
> this sort of issue is covered.
>
> See, it's all about expectations. When I worked at SLAC, there was a wise
> guy there who had removed the catalytic converter from his Scirocco and who
> though it clever to race up 280 from San Jose to SLAC at 100mph, sometimes
> slipstreaming behind another idiot doing the same. If I look in my
> rear-view mirror, I expect those I can see to be doing 70mph tops (that's
> what a lot of people did 30 years ago on 280), not 100, and to make
> judgements accordingly. Equally, I have certain expectations when I see a
> switch statement; it trying hard to look like if-elseif-etc is not one of
> them.
>
>
Just a minor rewrite of the original solution

switch (true) {
  case $count <= 7:
$boxes = 1;
break;
  case $count <= 14:
$boxes = 2;
break;
  default:
$boxes = 3;
break;
}


Now -- the idea of "switch" as "select on of some similar cases" in mind":
Is this really breaking any expectation? I think this one is a valid
example, when switch(true) doesn't break the semantic of the
switch-statement.
And if this is already the end of expectations, then (sorry for that) the
expectations are maybe slightly limited :X I don't say "please use
switch(true) wherever possible", but this one is nearly a prototype of a
valid use-case: You have several cases all depending on a single state (the
variable) and all of the same kind "<= constant".


Beside, because it is so extremely simple example, it can become even more
obvious

switch (true) {
  case $count <=  7:  $boxes = 1; break;
  case $count <= 14: $boxes = 2; break;
  default: $boxes = 3; break;
}


Guess some may argue about coding styles now, but thats not the questios
here (and anyway: Even coding styles could/should be broken, if it leads to
better code. Use it wisely ;)). And now: This _can_ break any expectation?


By the way, your car-expectation sounds dangerous...



> --
> Cheers  --  Tim
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>



-- 
github.com/KingCrunch


Re: [PHP] Re: Switch - Case Statement Questions

2012-11-19 Thread Stuart Dallas
On 19 Nov 2012, at 19:35, Tim Streater  wrote:

> On 18 Nov 2012 at 14:44, Jim Giner  wrote: 
> 
>> 2. Using Switch {ALWAYS FIRST CASE!!!}
>> 
>> //$boxes = 1;
>> //switch ($count) {
>> //case ($count > 14):
>> //$boxes = 3;
>> //break;
>> //case ($count > 7 && $count <= 14):
>> //$boxes = 2;
>> //break;
>> //case ($count <= 7):
>> //default :
>> //$boxes = 1;
>> //break;
>> //}
> 
> 
>> To answer Iñigo's comment - the OP's version is very much like an
>> If-ElseIF structure, and nothing like a Switch.
> 
> Just so. Perhaps those who are not grasping the point could re-read their 
> copy of "The Elements of Programming Style" by Kernighan and Plauger where 
> this sort of issue is covered.

And of course, nothing is allowed to have changed since 1978! Using a switch in 
that manner is normal for me. Should I change my style simply because you don't 
agree with it? If so, why don't you change your style because I don't agree 
with that? Code is art; there is no "right" way to do it. Can code be shown to 
be more efficient, elegant, faster, cleaner? Yes. Right or wrong? No.

> See, it's all about expectations. When I worked at SLAC, there was a wise guy 
> there who had removed the catalytic converter from his Scirocco and who 
> though it clever to race up 280 from San Jose to SLAC at 100mph, sometimes 
> slipstreaming behind another idiot doing the same. If I look in my rear-view 
> mirror, I expect those I can see to be doing 70mph tops (that's what a lot of 
> people did 30 years ago on 280), not 100, and to make judgements accordingly. 
> Equally, I have certain expectations when I see a switch statement; it trying 
> hard to look like if-elseif-etc is not one of them.

Why is it their fault for doing 100mph and not yours for assuming they're only 
doing 70mph? I'm not saying they're right to be doing 100mph, but I'm 
definitely saying you're wrong for assuming they're only doing 70mph.

Assumptions are the mother of all f*** ups.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Switch - Case Statement Questions

2012-11-19 Thread Tim Streater
On 18 Nov 2012 at 14:44, Jim Giner  wrote: 

> 2. Using Switch {ALWAYS FIRST CASE!!!}
>
> //$boxes = 1;
> //switch ($count) {
> //case ($count > 14):
> //$boxes = 3;
> //break;
> //case ($count > 7 && $count <= 14):
> //$boxes = 2;
> //break;
> //case ($count <= 7):
> //default :
> //$boxes = 1;
> //break;
> //}


> To answer Iñigo's comment - the OP's version is very much like an
> If-ElseIF structure, and nothing like a Switch.

Just so. Perhaps those who are not grasping the point could re-read their copy 
of "The Elements of Programming Style" by Kernighan and Plauger where this sort 
of issue is covered.

See, it's all about expectations. When I worked at SLAC, there was a wise guy 
there who had removed the catalytic converter from his Scirocco and who though 
it clever to race up 280 from San Jose to SLAC at 100mph, sometimes 
slipstreaming behind another idiot doing the same. If I look in my rear-view 
mirror, I expect those I can see to be doing 70mph tops (that's what a lot of 
people did 30 years ago on 280), not 100, and to make judgements accordingly. 
Equally, I have certain expectations when I see a switch statement; it trying 
hard to look like if-elseif-etc is not one of them.

--
Cheers  --  Tim

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: [PHP] Re: Switch - Case Statement Questions

2012-11-19 Thread Jim Giner

On 11/16/2012 8:33 PM, Iñigo Medina wrote:


On Fri, 16 Nov 2012, Jim Giner wrote:


On 11/16/2012 12:38 PM, Tim Streater wrote:

On 16 Nov 2012 at 12:10, Omar Muhsin  wrote:


Hello,

I was just wondering after writting the code in version 2 here
below, it turns
out in testing that it actually PHP is not validating the
expressions instead
always I get the first case.

1.Using nested if statement {THE INTENDED BEHAVIOR}:
if ($count > 14)
$boxes = 3;
elseif($count > 7 && $count <= 14)


You don't need the count<=14 part as you will *only* get there if
$count<=14.


$boxes = 2;
else
$boxes = 1;

2. Using Switch {ALWAYS FIRST CASE!!!}

//$boxes = 1;
//switch ($count) {
//case ($count > 14):
//$boxes = 3;
//break;
//case ($count > 7 && $count <= 14):
//$boxes = 2;
//break;
//case ($count <= 7):
//default :
//$boxes = 1;
//break;
//}


Does anyone know the answer why using the Switch it always execute
the first
case ?


As has been pointed out you need switch(true).

That's a strange way of writing a switch, IMO. You should be using
the if version.

--
Cheers  --  Tim


Maybe I'm way out of touch, but when I look at php.net for the syntax
of the switch statement I see:
switch($var){
case (value):
(do something)
case (other value):
(do something else)
}

IN your example, you are using a switch syntax that is nothing like
that.  I think that your "case " statements are not actually
evaluating the value of $count, but are themselves evaluating to a
true value that has nothing to do with $count.


That `switch` is evaluating to true each case. It's an alternative of using
if-elseif structure.

iñ



Here's the manual reference:
http://us.php.net/manual/en/control-structures.switch.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


My point was that the OP is using a widely different syntax for a Switch 
statement, one that I don't understand at all.  According to the Manual 
his statement should be:


 //switch ($count) {
 //case (> 14):
 //$boxes = 3;
 //break;
 //case (> 7):
 //$boxes = 2;
 //break;
 //default :
 //$boxes = 1;
 //break;
 //}


To answer Iñigo's comment - the OP's version is very much like an 
If-ElseIF structure, and nothing like a Switch.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] globbed includes?

2012-11-19 Thread tamouse mailing lists
On Nov 18, 2012 3:03 PM, "Adam Richardson"  wrote:
>
> On Sun, Nov 18, 2012 at 3:29 PM, tamouse mailing lists <
> tamouse.li...@gmail.com> wrote:
>
> > There are certain times I'd like to include all files in a given
> > directory (such as configuration stuff that is split out by type, a la
> > apache conf.d). Anyone have something handy that implements that?
> >
>
>
http://stackoverflow.com/questions/599670/how-to-include-all-php-files-from-a-directory
>
> --
> Nephtali:  A simple, flexible, fast, and security-focused PHP framework
> http://nephtaliproject.com

Yup that does what I want. Thanks, Adam


[PHP] Re: Variables with - in their name

2012-11-19 Thread Maciek Sokolewicz

On 19-11-2012 10:49, Alessandro Pellizzari wrote:

Il Sun, 18 Nov 2012 01:09:11 -0500, Ron Piggott ha scritto:


echo "\r\n";



It could be wrote:



You MUST disable register_globals in your php.ini

Once you have done that (and even before that...) you find your variable
in $_POST['distributor-42-2']

(or $_GET, depends on your form method)

Bye.


I agree it should *always* be disabled, however even when it is ENabled, 
$_POST or $_GET (and also $_REQUEST) will always include that key and 
value, regardless of the setting on register_globals.


In general though, it is most wise to DISable register_globals and code 
assuming it always will be disabled.


- Tul

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Variables with - in their name

2012-11-19 Thread Alessandro Pellizzari
Il Sun, 18 Nov 2012 01:09:11 -0500, Ron Piggott ha scritto:

> echo " $row['promo_code_suffix'] . "\" style=\"text-align: center;\">\r\n";

> It could be wrote:
> 
>  echo  $distributor-42-2;

You MUST disable register_globals in your php.ini

Once you have done that (and even before that...) you find your variable 
in $_POST['distributor-42-2']

(or $_GET, depends on your form method)

Bye.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php