Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
[PHP] Switches
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Heaplink" data-source="post: 212278" data-attributes="member: 8966"><p><span style="font-family: 'courier new'">if</span> and <span style="font-family: 'courier new'">switch</span> are different statements. The <span style="font-family: 'courier new'">switch</span> can meet one condition and goes through all cases until the condition is met. If statements can meet multiple conditions which need to be met and you have the possibility of doing something else, if the first conditions isn't met.</p><p> </p><p>It depends on what you wan't to do. Don't think about performance unless you got more than 1000 conditions to go through. SeanDavies' example is showing you usages.</p><p> </p><p>For your question about how switches work - it works like a... switch.</p><p> </p><p>Imagine a switch.</p><p>It has 5 states.</p><p>If we switch to Yellow we produce the color Yellow</p><p>If we switch to Green or Red we can produce anything else.</p><p>Else If the color is black or anything else we produce something else.</p><p> </p><p>[PHP]switch($color) {</p><p>case "Blue":</p><p>// $color is Blue we break here. We don't fall through it.</p><p>break;</p><p>case "Yellow":</p><p>// $color is Yellow</p><p>// If we return it ends the execution.</p><p>return;</p><p>case "Green":</p><p>case "Red":</p><p>// $color is either Green or Red</p><p>break;</p><p>case "Black":</p><p>default:</p><p>// $color is either Black or something else</p><p>// We can use default if no case is met.</p><p>// We can also fall through cases. Useful.</p><p>break;</p><p>}[/PHP]</p><p> </p><p>Equivalent <span style="font-family: 'courier new'">if</span> statement</p><p> </p><p>[PHP]if($color === "Blue") {</p><p> // $color is Blue.</p><p>} else if ($color === "Yellow") {</p><p> // $color is Yellow.</p><p> return;</p><p>} else if ($color === "Green" || $color === "Red") {</p><p> // $color is either Green or Red</p><p>} else {</p><p> // $color is something else (Notice!)</p><p>}[/PHP]</p></blockquote><p></p>
[QUOTE="Heaplink, post: 212278, member: 8966"] [FONT=courier new]if[/FONT] and [FONT=courier new]switch[/FONT] are different statements. The [FONT=courier new]switch[/FONT] can meet one condition and goes through all cases until the condition is met. If statements can meet multiple conditions which need to be met and you have the possibility of doing something else, if the first conditions isn't met. It depends on what you wan't to do. Don't think about performance unless you got more than 1000 conditions to go through. SeanDavies' example is showing you usages. For your question about how switches work - it works like a... switch. Imagine a switch. It has 5 states. If we switch to Yellow we produce the color Yellow If we switch to Green or Red we can produce anything else. Else If the color is black or anything else we produce something else. [PHP]switch($color) { case "Blue": // $color is Blue we break here. We don't fall through it. break; case "Yellow": // $color is Yellow // If we return it ends the execution. return; case "Green": case "Red": // $color is either Green or Red break; case "Black": default: // $color is either Black or something else // We can use default if no case is met. // We can also fall through cases. Useful. break; }[/PHP] Equivalent [FONT=courier new]if[/FONT] statement [PHP]if($color === "Blue") { // $color is Blue. } else if ($color === "Yellow") { // $color is Yellow. return; } else if ($color === "Green" || $color === "Red") { // $color is either Green or Red } else { // $color is something else (Notice!) }[/PHP] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
[PHP] Switches
Top