<?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; tutorial php mudah</title>
	<atom:link href="http://www.tutorialmelayu.com/tag/tutorial-php-mudah/feed" rel="self" type="application/rss+xml" />
	<link>http://www.tutorialmelayu.com</link>
	<description></description>
	<lastBuildDate>Sun, 06 Nov 2011 12:38:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Guna switch menggantikan if else</title>
		<link>http://www.tutorialmelayu.com/web-programming/php/guna-switch-menggantikan-if-else</link>
		<comments>http://www.tutorialmelayu.com/web-programming/php/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[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. &#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 if&#40;$destinasi == &#34;johor bahru&#34;&#41; echo &#34;mahu pergi johor bahru &#60;br&#62;&#34;; else if&#40;$destinasi == &#34;kota bharu&#34;&#41; echo [...]]]></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"><table><tr id="p943"><td class="code" id="p94code3"><pre class="php" style="font-family:monospace;"><span style="color: #dfc484; font-weight: bold;">&lt;?php</span>
<span style="color: #dfc484;">$destinasi</span> <span style="color: #eeeeee;">=</span> <span style="color: #ff8400;">&quot;kota bharu&quot;</span><span style="color: #eeeeee;">;</span>
&nbsp;
<span style="color: #e8ed97;">if</span><span style="color: #eeeeee;">&#40;</span><span style="color: #dfc484;">$destinasi</span> <span style="color: #eeeeee;">==</span> <span style="color: #ff8400;">&quot;kuala lumpur&quot;</span><span style="color: #eeeeee;">&#41;</span>
   <span style="color: #e8ed97;">echo</span> <span style="color: #ff8400;">&quot;mahu pergi kuala lumpur &lt;br&gt;&quot;</span><span style="color: #eeeeee;">;</span>
<span style="color: #e8ed97;">else</span> <span style="color: #e8ed97;">if</span><span style="color: #eeeeee;">&#40;</span><span style="color: #dfc484;">$destinasi</span> <span style="color: #eeeeee;">==</span> <span style="color: #ff8400;">&quot;johor bahru&quot;</span><span style="color: #eeeeee;">&#41;</span>
   <span style="color: #e8ed97;">echo</span> <span style="color: #ff8400;">&quot;mahu pergi johor bahru &lt;br&gt;&quot;</span><span style="color: #eeeeee;">;</span>
<span style="color: #e8ed97;">else</span> <span style="color: #e8ed97;">if</span><span style="color: #eeeeee;">&#40;</span><span style="color: #dfc484;">$destinasi</span> <span style="color: #eeeeee;">==</span> <span style="color: #ff8400;">&quot;kota bharu&quot;</span><span style="color: #eeeeee;">&#41;</span>
   <span style="color: #e8ed97;">echo</span> <span style="color: #ff8400;">&quot;mahu pergi kota bharu &lt;br&gt;&quot;</span><span style="color: #eeeeee;">;</span>
<span style="color: #e8ed97;">else</span> <span style="color: #e8ed97;">if</span><span style="color: #eeeeee;">&#40;</span><span style="color: #dfc484;">$destinasi</span> <span style="color: #eeeeee;">==</span> <span style="color: #ff8400;">&quot;kuantan&quot;</span><span style="color: #eeeeee;">&#41;</span>
   <span style="color: #e8ed97;">echo</span> <span style="color: #ff8400;">&quot;mahu pergi kuantan &lt;br&gt;&quot;</span><span style="color: #eeeeee;">;</span>
<span style="color: #e8ed97;">else</span>
   <span style="color: #e8ed97;">echo</span> <span style="color: #ff8400;">&quot;tak pegi mana mana&quot;</span><span style="color: #eeeeee;">;</span> 
<span style="color: #eeeeee; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Boleh digantikan dengan</p>

<div class="wp_codebox"><table><tr id="p944"><td class="code" id="p94code4"><pre class="php" style="font-family:monospace;"><span style="color: #dfc484; font-weight: bold;">&lt;?php</span>
<span style="color: #dfc484;">$destinasi</span> <span style="color: #eeeeee;">=</span> <span style="color: #ff8400;">&quot;kota bharu&quot;</span><span style="color: #eeeeee;">;</span>
<span style="color: #e8ed97;">switch</span><span style="color: #eeeeee;">&#40;</span><span style="color: #dfc484;">$destinasi</span><span style="color: #eeeeee;">&#41;</span><span style="color: #eeeeee;">&#123;</span>
    <span style="color: #e8ed97;">case</span> <span style="color: #ff8400;">&quot;kuala lumpur&quot;</span> <span style="color: #eeeeee;">:</span> 
        <span style="color: #e8ed97;">echo</span> <span style="color: #ff8400;">&quot;mahu pergi kuala lumpur&quot;</span><span style="color: #eeeeee;">;</span> 
    <span style="color: #e8ed97;">break</span><span style="color: #eeeeee;">;</span>
    <span style="color: #e8ed97;">case</span> <span style="color: #ff8400;">&quot;johor bahru&quot;</span> <span style="color: #eeeeee;">:</span> 
        <span style="color: #e8ed97;">echo</span> <span style="color: #ff8400;">&quot;mahu pergi johor bahru&quot;</span><span style="color: #eeeeee;">;</span> 
    <span style="color: #e8ed97;">break</span><span style="color: #eeeeee;">;</span>
    <span style="color: #e8ed97;">case</span> <span style="color: #ff8400;">&quot;kota bharu&quot;</span> <span style="color: #eeeeee;">:</span> 
        <span style="color: #e8ed97;">echo</span> <span style="color: #ff8400;">&quot;mahu pergi kota bharu&quot;</span><span style="color: #eeeeee;">;</span> 
    <span style="color: #e8ed97;">break</span><span style="color: #eeeeee;">;</span>
    <span style="color: #e8ed97;">case</span> <span style="color: #ff8400;">&quot;kuantan&quot;</span> <span style="color: #eeeeee;">:</span> 
        <span style="color: #e8ed97;">echo</span> <span style="color: #ff8400;">&quot;mahu pergi kuantan&quot;</span><span style="color: #eeeeee;">;</span> 
    <span style="color: #e8ed97;">break</span><span style="color: #eeeeee;">;</span>
    <span style="color: #e8ed97;">default</span> <span style="color: #eeeeee;">:</span>
        <span style="color: #e8ed97;">echo</span> <span style="color: #ff8400;">&quot;tak pegi mana mana&quot;</span><span style="color: #eeeeee;">;</span> 
    <span style="color: #e8ed97;">break</span><span style="color: #eeeeee;">;</span>
<span style="color: #eeeeee;">&#125;</span>
<span style="color: #eeeeee; 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/php/guna-switch-menggantikan-if-else/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

