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="thomaslackner" data-source="post: 212744" data-attributes="member: 35107"><p>While on the subject of switch statements, another cool thing to try out is using an ARRAY to act as a switch statement or a big pile of IF statements. That sounds crazy, but imagine this:</p><p> </p><p> </p><p>[PHP]switch ($color_number) {</p><p>case 1:</p><p> return 'red';</p><p> break;</p><p>case 2:</p><p> return 'green';</p><p> break;</p><p>case 3:</p><p> return 'blue';</p><p> break;</p><p>}</p><p> </p><p> [/PHP]</p><p> </p><p>It's pretty common to use switch statements like this, to map one value to another. But there's actually an easier (and clearer) way:</p><p> </p><p>[PHP]$color_numbers_to_names = array(</p><p> 1 => 'red',</p><p> 2 => 'green',</p><p> 3 => 'blue');</p><p>return $color_numbers_to_names[$color_number];</p><p>[/PHP]</p><p> </p><p>This is great because it makes you think about what *data* your program is working with, not just what the logic is. Often times in large programs, the data contains most of the logic, rather than the code itself (in this example, imagine if you had to make an admin application to allow people to change color number assignments, for example).</p></blockquote><p></p>
[QUOTE="thomaslackner, post: 212744, member: 35107"] While on the subject of switch statements, another cool thing to try out is using an ARRAY to act as a switch statement or a big pile of IF statements. That sounds crazy, but imagine this: [PHP]switch ($color_number) { case 1: return 'red'; break; case 2: return 'green'; break; case 3: return 'blue'; break; } [/PHP] It's pretty common to use switch statements like this, to map one value to another. But there's actually an easier (and clearer) way: [PHP]$color_numbers_to_names = array( 1 => 'red', 2 => 'green', 3 => 'blue'); return $color_numbers_to_names[$color_number]; [/PHP] This is great because it makes you think about what *data* your program is working with, not just what the logic is. Often times in large programs, the data contains most of the logic, rather than the code itself (in this example, imagine if you had to make an admin application to allow people to change color number assignments, for example). [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
[PHP] Switches
Top