You are here: Re: switch (true) {} without a break statement « PHP Programming Language « IT news, forums, messages
Re: switch (true) {} without a break statement

Posted by My Pet Programmer on 12/28/07 16:39

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

 

Navigation:

[Reply to this 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

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