switch (true) {} without a break statement

This is a discussion on switch (true) {} without a break statement within the PHP Language forums, part of the PHP Programming Forums category; Hello everybody, The switch statement is not my favorite control structure but I figured it would fit this situation best ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-28-2007
Marijn
 
Posts: n/a
Default switch (true) {} without a break statement

Hello everybody,

The switch statement is not my favorite control structure but I
figured it would fit this situation best given fall through behavior.
So I read the documentation, checked my code a thousand times but it
just _seems_ to work incorrect. The code is as follows.

public function addTimeMarker(){
$_timeparts = explode(' ', microtime());
$_markersCount = count($this->_timeMarkers);
switch (true) {
case ($_markersCount >= 0) :
//always add a new marker
$this->_timeMarkers[] = $_timeparts[1] .
substr($_timeparts[0],1);
case ($_markersCount >= 1) :
//if one or more markers already existed add time elapsed
since first marker to the log
$_durationSinceStart = bcsub($this-
>_timeMarkers[$_markersCount - 1], $this->_timeMarkers[0], 6);

ErrorLog::addMessage('Request took ' .
$_durationSinceStart . 'seconds until now', 75);
case ($_markersCount >= 2) :
//if two or more markers already existed add time elapsed
since last marker to the log
$_durationSincePreviousMarker = bcsub($this-
>_timeMarkers[$_markersCount - 1], $this->_timeMarkers[$_markersCount

- 2], 6);
ErrorLog::addMessage('Request took ' .
$_durationSincePreviousMarker . 'seconds since previous marker', 75);
break;
}
}

For some reason it will execute them all on the very first run while
$_markersCount is zero... What am I doing wrong here or why is it
doing things like this?

Thanks in advance,

Marijn





Reply With Quote
  #2 (permalink)  
Old 12-28-2007
My Pet Programmer
 
Posts: n/a
Default Re: switch (true) {} without a break statement

Marijn said:
> Hello everybody,
>
> The switch statement is not my favorite control structure but I
> figured it would fit this situation best given fall through behavior.
> So I read the documentation, checked my code a thousand times but it
> just _seems_ to work incorrect. The code is as follows.
>
> public function addTimeMarker(){
> $_timeparts = explode(' ', microtime());
> $_markersCount = count($this->_timeMarkers);
> switch (true) {
> case ($_markersCount >= 0) :
> //always add a new marker
> $this->_timeMarkers[] = $_timeparts[1] .
> substr($_timeparts[0],1);
> case ($_markersCount >= 1) :
> //if one or more markers already existed add time elapsed
> since first marker to the log
> $_durationSinceStart = bcsub($this-
>> _timeMarkers[$_markersCount - 1], $this->_timeMarkers[0], 6);

> ErrorLog::addMessage('Request took ' .
> $_durationSinceStart . 'seconds until now', 75);
> case ($_markersCount >= 2) :
> //if two or more markers already existed add time elapsed
> since last marker to the log
> $_durationSincePreviousMarker = bcsub($this-
>> _timeMarkers[$_markersCount - 1], $this->_timeMarkers[$_markersCount

> - 2], 6);
> ErrorLog::addMessage('Request took ' .
> $_durationSincePreviousMarker . 'seconds since previous marker', 75);
> break;
> }
> }
>
> For some reason it will execute them all on the very first run while
> $_markersCount is zero... What am I doing wrong here or why is it
> doing things like this?
>
> Thanks in advance,
>
> Marijn
>
>
>
>
>

You're switching true. ALL of the cases are always true.

Switch the value you want to test.

Tested on:

$age = 20;

switch(true) {
case "10":
print "Yup!";
break;
}

~A!

--
Anthony Levensalor
anthony@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Reply With Quote
  #3 (permalink)  
Old 12-28-2007
Michael Fesser
 
Posts: n/a
Default Re: switch (true) {} without a break statement

..oO(Marijn)

>The switch statement is not my favorite control structure but I
>figured it would fit this situation best given fall through behavior.
>So I read the documentation, checked my code a thousand times but it
>just _seems_ to work incorrect. The code is as follows.
>
>public function addTimeMarker(){
> $_timeparts = explode(' ', microtime());
> $_markersCount = count($this->_timeMarkers);
> switch (true) {
> case ($_markersCount >= 0) :
> //always add a new marker
> $this->_timeMarkers[] = $_timeparts[1] .
>substr($_timeparts[0],1);
> case ($_markersCount >= 1) :
> //if one or more markers already existed add time elapsed
>since first marker to the log
> $_durationSinceStart = bcsub($this-
>>_timeMarkers[$_markersCount - 1], $this->_timeMarkers[0], 6);

> ErrorLog::addMessage('Request took ' .
>$_durationSinceStart . 'seconds until now', 75);
> case ($_markersCount >= 2) :
> //if two or more markers already existed add time elapsed
>since last marker to the log
> $_durationSincePreviousMarker = bcsub($this-
>>_timeMarkers[$_markersCount - 1], $this->_timeMarkers[$_markersCount

>- 2], 6);
> ErrorLog::addMessage('Request took ' .
>$_durationSincePreviousMarker . 'seconds since previous marker', 75);
> break;
> }
>}


Every 'case' block should be terminated with a break. Have a look at the
manual on how to properly use a 'switch'. An example like above is given
there.

And if you find yourself using a switch(TRUE), you might want to
consider using 'if-else' statements instead for better code style.
switch(TRUE) is a bit abusive and only possible because PHP allows
complex expressions in the case statements. In other languages this
would not work.

Micha
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:35 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0