<?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>code-diesel &#187; style</title>
	<atom:link href="http://www.codediesel.com/tag/style/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>The benefits of colon syntax for control structures</title>
		<link>http://www.codediesel.com/php/the-benefits-of-the-colon-syntax-for-control-structures/</link>
		<comments>http://www.codediesel.com/php/the-benefits-of-the-colon-syntax-for-control-structures/#comments</comments>
		<pubDate>Sun, 11 Jan 2009 11:25:15 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[style]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=150</guid>
		<description><![CDATA[Most PHP programmes may not be familiar with the colon syntax used for control structures. If you regularly code in wordpress than you already are acquainted with it. PHP offers a alternative syntax for some of its control structures- if, while, for, foreach, and switch, where you change the opening brace to a colon (:) [...]]]></description>
			<content:encoded><![CDATA[<p>Most PHP programmes may not be familiar with the colon syntax used for control structures. If you regularly code in wordpress than you already are acquainted with it.</p>
<p>PHP offers a alternative syntax for some of its control structures- if, while, for, foreach, and switch, where you change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively. The main advantage I&#8217;ve found in using this syntax is that it makes deeply nested code intelligible to read. If you have a deeply nested code it sometime becomes hard to pair the starting and ending brace. With the colon syntax you just have to match a &#8216;if&#8217; with a &#8216;endif&#8217; or a &#8216;for&#8217; with a &#8216;endfor&#8217;.  When you are mixing HTML with PHP in web pages the code can become quite dense and confusing if it uses a lot of braces. At that time the colon syntax can come quite handy.<br />
<span id="more-150"></span><br />
A example function using the colon syntax is shown below:</p>

<div class="wp_codebox"><table><tr id="p1502"><td class="code" id="p150code2"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_array_ordered<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
        <span style="color: #000088;">$total_elements</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_elements</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
                <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
                <span style="color: #000088;">$total_elements</span><span style="color: #339933;">--;</span>
            <span style="color: #000000; font-weight: bold;">else</span><span style="color: #339933;">:</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">endif</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">endwhile</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">endif</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$test_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> is_array_ordered<span style="color: #009900;">&#40;</span><span style="color: #000088;">$test_array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/the-benefits-of-the-colon-syntax-for-control-structures/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

