<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
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:series="http://unfoldingneurons.com/"
> <channel><title>Comments on: Learning Ternary Operators &#8212; Tips &amp;&#160;Tricks</title> <atom:link href="http://davidwalsh.name/learning-ternary-operators-tips-tricks/feed" rel="self" type="application/rss+xml" /><link>http://davidwalsh.name/learning-ternary-operators-tips-tricks</link> <description>Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</description> <lastBuildDate>Thu, 09 Feb 2012 15:40:33 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3</generator> <item><title>By: monster87</title><link>http://davidwalsh.name/learning-ternary-operators-tips-tricks/comment-page-1#comment-2767</link> <dc:creator>monster87</dc:creator> <pubDate>Tue, 19 Aug 2008 13:08:44 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/learning-ternary-operators-tips-tricks#comment-2767</guid> <description>&lt;blockquote&gt;
Nice write up! I used to use the
parentheses the same as in your
example:
$dynamic = ($language == &#039;php&#039; ? true
: false);
But was &quot;mildly scolded&quot; by a JS guru
much smarter than I. He said it should
be:
$dynamic = ($language == &#039;php&#039;) ? true
: false;
Now technically, either way is
&quot;correct&quot; since JS allows both ways.
But I found myself agreeing with said
guru, mainly that the logic test
portion should be in the parentheses.
Example:
if ($language == &#039;php&#039;) { // code }
else { // code }
&lt;/blockquote&gt;Even simpler would be:
$dynamic = $language == &#039;php&#039; ? true : false;or even:
$dynamic = $language == &#039;php&#039;;in JS this works the same way.And for anyone who is interested.
Although it seems to miss in php, javascript does have an interesting even shorter expression for when working with if it&#039;s false then use this defaultphp:
$somevar = $othervar ? $othervar : &quot;default&quot;;javascript:
$somevar = $othervar &#124;&#124; &quot;default&quot;;(in php $somevar will evaluate to &#039;true&#039; since a filled string (&quot;default&quot;) is always &#039;true&#039; in a or(&#124;&#124;)-situation)</description> <content:encoded><![CDATA[<blockquote><p> Nice write up! I used to use the<br
/> parentheses the same as in your<br
/> example:</p><p> $dynamic = ($language == &#8216;php&#8217; ? true<br
/> : false);</p><p> But was &#8220;mildly scolded&#8221; by a JS guru<br
/> much smarter than I. He said it should<br
/> be:</p><p> $dynamic = ($language == &#8216;php&#8217;) ? true<br
/> : false;</p><p> Now technically, either way is<br
/> &#8220;correct&#8221; since JS allows both ways.<br
/> But I found myself agreeing with said<br
/> guru, mainly that the logic test<br
/> portion should be in the parentheses.<br
/> Example:</p><p> if ($language == &#8216;php&#8217;) { // code }<br
/> else { // code }</p></blockquote><p>Even simpler would be:<br
/> $dynamic = $language == &#8216;php&#8217; ? true : false;</p><p>or even:<br
/> $dynamic = $language == &#8216;php&#8217;;</p><p>in JS this works the same way.</p><p>And for anyone who is interested.<br
/> Although it seems to miss in php, javascript does have an interesting even shorter expression for when working with if it&#8217;s false then use this default</p><p>php:<br
/> $somevar = $othervar ? $othervar : &#8220;default&#8221;;</p><p>javascript:<br
/> $somevar = $othervar || &#8220;default&#8221;;</p><p>(in php $somevar will evaluate to &#8216;true&#8217; since a filled string (&#8220;default&#8221;) is always &#8216;true&#8217; in a or(||)-situation)</p> ]]></content:encoded> </item> <item><title>By: TedInAK</title><link>http://davidwalsh.name/learning-ternary-operators-tips-tricks/comment-page-1#comment-647</link> <dc:creator>TedInAK</dc:creator> <pubDate>Mon, 14 Apr 2008 02:07:04 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/learning-ternary-operators-tips-tricks#comment-647</guid> <description>Nice write up!  I used to use the parentheses the same as in your example:$dynamic = ($language == &#039;php&#039; ? true : false);But was &quot;mildly scolded&quot; by a JS guru much smarter than I.  He said it should be:$dynamic = ($language == &#039;php&#039;) ? true : false;Now technically, either way is &quot;correct&quot; since JS allows both ways.  But I found myself agreeing with said guru, mainly that the logic test portion should be in the parentheses.  Example:if ($language == &#039;php&#039;) {
// code
} else {
// code
}So to his way of thinking (and now mine), since in this structure it&#039;s just the logical test that is within the parentheses, so should it be in the ternary structure.</description> <content:encoded><![CDATA[<p>Nice write up!  I used to use the parentheses the same as in your example:</p><p>$dynamic = ($language == &#8216;php&#8217; ? true : false);</p><p>But was &#8220;mildly scolded&#8221; by a JS guru much smarter than I.  He said it should be:</p><p>$dynamic = ($language == &#8216;php&#8217;) ? true : false;</p><p>Now technically, either way is &#8220;correct&#8221; since JS allows both ways.  But I found myself agreeing with said guru, mainly that the logic test portion should be in the parentheses.  Example:</p><p>if ($language == &#8216;php&#8217;) {<br
/> // code<br
/> } else {<br
/> // code<br
/> }</p><p>So to his way of thinking (and now mine), since in this structure it&#8217;s just the logical test that is within the parentheses, so should it be in the ternary structure.</p> ]]></content:encoded> </item> <item><title>By: david</title><link>http://davidwalsh.name/learning-ternary-operators-tips-tricks/comment-page-1#comment-509</link> <dc:creator>david</dc:creator> <pubDate>Mon, 17 Mar 2008 17:46:34 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/learning-ternary-operators-tips-tricks#comment-509</guid> <description>A switch could work there too Dagnan.  Good point!</description> <content:encoded><![CDATA[<p>A switch could work there too Dagnan.  Good point!</p> ]]></content:encoded> </item> <item><title>By: Dagnan</title><link>http://davidwalsh.name/learning-ternary-operators-tips-tricks/comment-page-1#comment-511</link> <dc:creator>Dagnan</dc:creator> <pubDate>Mon, 17 Mar 2008 17:32:21 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/learning-ternary-operators-tips-tricks#comment-511</guid> <description>The example with intermediate variables is interresting, but i&#039;d better use a switch structure instead. What do you think?But thanks for the coding tips :)</description> <content:encoded><![CDATA[<p>The example with intermediate variables is interresting, but i&#8217;d better use a switch structure instead. What do you think?</p><p>But thanks for the coding tips :)</p> ]]></content:encoded> </item> <item><title>By: Bouchard, Keven</title><link>http://davidwalsh.name/learning-ternary-operators-tips-tricks/comment-page-1#comment-513</link> <dc:creator>Bouchard, Keven</dc:creator> <pubDate>Mon, 17 Mar 2008 16:36:50 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/learning-ternary-operators-tips-tricks#comment-513</guid> <description>Nice article! that&#039;s something I wanted to learn/practice to save some time and clear my code when I have to use simple if/else, I&#039;ll try to add this to my coding habits ;)</description> <content:encoded><![CDATA[<p>Nice article! that&#8217;s something I wanted to learn/practice to save some time and clear my code when I have to use simple if/else, I&#8217;ll try to add this to my coding habits ;)</p> ]]></content:encoded> </item> <item><title>By: Will</title><link>http://davidwalsh.name/learning-ternary-operators-tips-tricks/comment-page-1#comment-512</link> <dc:creator>Will</dc:creator> <pubDate>Mon, 17 Mar 2008 16:30:09 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/learning-ternary-operators-tips-tricks#comment-512</guid> <description>Thanks, I use a lot (probably unnecessary) if/else statements which could be cut down.</description> <content:encoded><![CDATA[<p>Thanks, I use a lot (probably unnecessary) if/else statements which could be cut down.</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 1/17 queries in 0.009 seconds using disk: basic
Object Caching 599/614 objects using disk: basic

Served from: davidwalsh.name @ 2012-02-09 11:37:41 -->
