<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tutorial Melayu &#187; switch ganti if else</title>
	<atom:link href="http://www.tutorialmelayu.com/tag/switch-ganti-if-else/feed" rel="self" type="application/rss+xml" />
	<link>http://www.tutorialmelayu.com</link>
	<description></description>
	<lastBuildDate>Sat, 04 Sep 2010 06:30:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Guna switch menggantikan if else</title>
		<link>http://www.tutorialmelayu.com/web-programming/guna-switch-menggantikan-if-else</link>
		<comments>http://www.tutorialmelayu.com/web-programming/guna-switch-menggantikan-if-else#comments</comments>
		<pubDate>Sat, 28 Mar 2009 23:50:49 +0000</pubDate>
		<dc:creator>mdpai</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[web programming]]></category>
		<category><![CDATA[php asas]]></category>
		<category><![CDATA[switch ganti if else]]></category>
		<category><![CDATA[tutorial php mudah]]></category>

		<guid isPermaLink="false">http://www.tutorialmelayu.com/?p=94</guid>
		<description><![CDATA[Sekiranya anda mempunyai struktur if else yang kompleks seperti berikut, anda boleh menggantikannya dengan struktur switch yang jauh lebih mudah untuk difahami. ?View Code PHP1 2 3 4 5 6 7 8 9 10 11 12 13 14 &#60;?php $destinasi = &#34;kota bharu&#34;; &#160; if&#40;$destinasi == &#34;kuala lumpur&#34;&#41; echo &#34;mahu pergi kuala lumpur &#60;br&#62;&#34;; else [...]]]></description>
			<content:encoded><![CDATA[<p>Sekiranya anda mempunyai struktur if else yang kompleks seperti berikut, anda boleh menggantikannya dengan struktur switch yang jauh lebih mudah untuk difahami.</p>
<p><span id="more-94"></span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p94code3'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p943"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p94code3"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$destinasi</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;kota bharu&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destinasi</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;kuala lumpur&quot;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;mahu pergi kuala lumpur &lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destinasi</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;johor bahru&quot;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;mahu pergi johor bahru &lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destinasi</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;kota bharu&quot;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;mahu pergi kota bharu &lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destinasi</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;kuantan&quot;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;mahu pergi kuantan &lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;tak pegi mana mana&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Boleh digantikan dengan</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p94code4'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p944"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p94code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$destinasi</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;kota bharu&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destinasi</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;kuala lumpur&quot;</span> <span style="color: #339933;">:</span> 
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;mahu pergi kuala lumpur&quot;</span><span style="color: #339933;">;</span> 
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;johor bahru&quot;</span> <span style="color: #339933;">:</span> 
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;mahu pergi johor bahru&quot;</span><span style="color: #339933;">;</span> 
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;kota bharu&quot;</span> <span style="color: #339933;">:</span> 
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;mahu pergi kota bharu&quot;</span><span style="color: #339933;">;</span> 
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;kuantan&quot;</span> <span style="color: #339933;">:</span> 
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;mahu pergi kuantan&quot;</span><span style="color: #339933;">;</span> 
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">default</span> <span style="color: #339933;">:</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;tak pegi mana mana&quot;</span><span style="color: #339933;">;</span> 
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Jika anda perhatikan setiap case diakhiri dengan break; . Ianya untuk mengelakkan case yang seterusnya diexecute. Pastikan juga anda meletakkan case default, case ini akan diexecute jika struktur switch tidak menjumpai case yang sama dengan nilai $destinasi. Walaupun tidak wajib, namun saya sarankan anda memasukkan default setiap kali anda menggunakan switch.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tutorialmelayu.com/web-programming/guna-switch-menggantikan-if-else/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
