<?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; if else</title>
	<atom:link href="http://www.tutorialmelayu.com/tag/if-else/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>if else &amp; ternary conditionals</title>
		<link>http://www.tutorialmelayu.com/web-programming/php/if-else-ternary-conditionals</link>
		<comments>http://www.tutorialmelayu.com/web-programming/php/if-else-ternary-conditionals#comments</comments>
		<pubDate>Sun, 08 Feb 2009 09:00:20 +0000</pubDate>
		<dc:creator>mdpai</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[belajar php melayu]]></category>
		<category><![CDATA[if else]]></category>
		<category><![CDATA[ternary operator]]></category>

		<guid isPermaLink="false">http://www.tutorialmelayu.com/?p=58</guid>
		<description><![CDATA[Kod di bawah bermaksud, jika condition true, buat statement1, jika false buat statement2. if &#40;condition&#41; &#123; statement1; &#125; else &#123; statement2; &#125; Mari kita lihat contoh sebenar di dalam PHP; &#60;?php if &#40;date&#40;&#34;G&#34;&#41; &#60; 12&#41; &#123; echo 'Selamat Pagi'; &#125; else &#123; echo 'Selamat Petang'; &#125; ?&#62; Namun, statement if else ini boleh disingkatkan lagi [...]]]></description>
			<content:encoded><![CDATA[<p>Kod di bawah bermaksud, jika condition true, buat statement1, jika false buat statement2. </p>
<p><span id="more-58"></span></p>

<div class="wp_codebox"><table><tr id="p585"><td class="code" id="p58code5"><pre class="php" style="font-family:monospace;"><span style="color: #e8ed97;">if</span> <span style="color: #eeeeee;">&#40;</span>condition<span style="color: #eeeeee;">&#41;</span> <span style="color: #eeeeee;">&#123;</span>
   statement1<span style="color: #eeeeee;">;</span>
<span style="color: #eeeeee;">&#125;</span> <span style="color: #e8ed97;">else</span> <span style="color: #eeeeee;">&#123;</span>
   statement2<span style="color: #eeeeee;">;</span>
<span style="color: #eeeeee;">&#125;</span></pre></td></tr></table></div>

<p>Mari kita lihat contoh sebenar di dalam PHP;</p>

<div class="wp_codebox"><table><tr id="p586"><td class="code" id="p58code6"><pre class="php" style="font-family:monospace;"><span style="color: #dfc484; font-weight: bold;">&lt;?php</span>
<span style="color: #e8ed97;">if</span> <span style="color: #eeeeee;">&#40;</span><span style="color: #dfc484;">date</span><span style="color: #eeeeee;">&#40;</span><span style="color: #ff8400;">&quot;G&quot;</span><span style="color: #eeeeee;">&#41;</span> <span style="color: #eeeeee;">&lt;</span> <span style="color: #ff8400;">12</span><span style="color: #eeeeee;">&#41;</span> <span style="color: #eeeeee;">&#123;</span>
<span style="color: #e8ed97;">echo</span> <span style="color: #ff8400;">'Selamat Pagi'</span><span style="color: #eeeeee;">;</span>
<span style="color: #eeeeee;">&#125;</span> <span style="color: #e8ed97;">else</span> <span style="color: #eeeeee;">&#123;</span>
<span style="color: #e8ed97;">echo</span> <span style="color: #ff8400;">'Selamat Petang'</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>Namun, statement if else ini boleh disingkatkan lagi dengan menggunakan operator ternary. Ternary Conditionals (menggunakan operator ternary) adalah salah satu cara untuk menjimatkan masa anda menulis code PHP. Strukturnya yang kelihatan <del datetime="2009-02-08T05:19:14+00:00">susah</del> pelik untuk difahami menyebabkan ianya tidak popular dikalangan programmer.<br />
Contoh di atas boleh ditulis semula dengan;</p>

<div class="wp_codebox"><table><tr id="p587"><td class="code" id="p58code7"><pre class="php" style="font-family:monospace;"><span style="color: #dfc484; font-weight: bold;">&lt;?php</span>
<span style="color: #e8ed97;">echo</span> <span style="color: #eeeeee;">&#40;</span><span style="color: #dfc484;">date</span><span style="color: #eeeeee;">&#40;</span><span style="color: #ff8400;">&quot;G&quot;</span><span style="color: #eeeeee;">&#41;</span> <span style="color: #eeeeee;">&lt;</span> <span style="color: #ff8400;">12</span><span style="color: #eeeeee;">&#41;</span> ? <span style="color: #ff8400;">'Selamat Pagi'</span> <span style="color: #eeeeee;">:</span> <span style="color: #ff8400;">'Selamat Petang'</span><span style="color: #eeeeee;">;</span>
<span style="color: #eeeeee; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>atau</p>

<div class="wp_codebox"><table><tr id="p588"><td class="code" id="p58code8"><pre class="php" style="font-family:monospace;"><span style="color: #dfc484; font-weight: bold;">&lt;?php</span>
<span style="color: #dfc484;">$greeting</span> <span style="color: #eeeeee;">=</span> <span style="color: #eeeeee;">&#40;</span><span style="color: #dfc484;">date</span><span style="color: #eeeeee;">&#40;</span><span style="color: #ff8400;">&quot;G&quot;</span><span style="color: #eeeeee;">&#41;</span> <span style="color: #eeeeee;">&lt;</span> <span style="color: #ff8400;">12</span><span style="color: #eeeeee;">&#41;</span> ? <span style="color: #ff8400;">'Selamat Pagi'</span> <span style="color: #eeeeee;">:</span> <span style="color: #ff8400;">'Selamat Petang'</span><span style="color: #eeeeee;">;</span>
<span style="color: #e8ed97;">echo</span> <span style="color: #dfc484;">$greeting</span><span style="color: #eeeeee;">;</span>
<span style="color: #eeeeee; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><a href="http://www.tutorialmelayu.com/wp-content/uploads/2009/02/800px-php-n_logosvg2.png"><img src="http://www.tutorialmelayu.com/wp-content/uploads/2009/02/800px-php-n_logosvg2.png" alt="800px-php-n_logosvg2" title="800px-php-n_logosvg2" width="256" height="134" class="alignnone size-full wp-image-74" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tutorialmelayu.com/web-programming/php/if-else-ternary-conditionals/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

