{"id":1496,"date":"2012-12-06T11:06:51","date_gmt":"2012-12-06T19:06:51","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1496"},"modified":"2012-12-07T17:28:07","modified_gmt":"2012-12-08T01:28:07","slug":"ternary-operator","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1496","title":{"rendered":"Ternary operator"},"content":{"rendered":"<p>Ternary operators are a concise way to write conditional statements that have two possible outcomes. Rather than writing a longer series of if\/then\/else statements you can write one line that makes it clear what the two choices are.<\/p>\n<p>In this example from iOS, I use a string for direction\u2014either clockwise or counterclockwise\u2014and translate it to a number for use in the formula. That way I don&#8217;t have to remember whether -1 is clockwise or counterclockwise when calling the method. I can use natural language to call the method and let the ternary operator take care of the conversion to the value I need in my formula. And I can change my formula at a later date without having to go back and find all the method calls.<\/p>\n<p>Here\u2019s the method call and the operator<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\n- (void)spin:(NSString *)direction withDuration:(float)duration withScale:(float)scale {\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;int rotation = ([direction isEqualToString:@&quot;clockWise&quot;] ? 1 : -1);\n<\/code><\/pre><\/p>\n<p>And then use rotation later to determine which way the object rotates<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nview.transform = CGAffineTransformRotate(CGAffineTransformScale(transform, 1.0, 1.0), rotation * 2*M_PI\/3);\n<\/code><\/pre><\/p>\n<p>Here\u2019s another example, where I want to pass in a value, but make sure that it isn\u2019t less than one. In this case I\u2019m passing in an integer and rather than doing a series of complicated if\/then\/else statements I just put the ternary operator in where the integer goes.<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nself.showRewards = [[ShowRewards alloc] initWithParentView:self.view withLevel:(rewardLevel &gt; 0 ? rewardLevel : 1) ];\n<\/code><\/pre><br \/>\nI also use it in PHP code for plurals. Something like this is what I use.<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\n$text = &quot;The update was successful. $recordsUpdated &quot; . ($recordsUpdated &gt; 1 ? &#039;records were updated.&#039; : &#039;record was updated.&#039;);\necho $text;\n<\/code><\/pre><\/p>\n<p>And I use it to write one set of code that works for two inputs. In this case I have a page that displays all of the titles that are downloadable from Gumroad. Since people are only interested in the Mac or Windows version, I  put them on two different pages\u2014but I use the same code. The first part reads in the page type from the URL and puts up a title for Macintosh or Windows.<\/p>\n<p><pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nif ( isset($_GET[&#039;page&#039;]) ) { $MacWin&nbsp;&nbsp;= mysql_real_escape_string($_GET[&#039;page&#039;]); }&nbsp;&nbsp;else { $MacWin&nbsp;&nbsp;= &#039;Win&#039;;} \n\necho &quot;&lt;div id=&#039;wideMargins&#039;&gt;&quot;;\n$MacintoshWindows = ($MacWin == &#039;Mac&#039; ? &#039;Macintosh&#039; : &#039;Windows&#039;);\necho &quot;&lt;h2 class=&#039;NewSection&#039;&gt;Download $MacintoshWindows Compatible Titles from Gumroad&lt;\/h2&gt;&quot;;\n<\/code><\/pre><\/p>\n<p>Then in the SQL statements I pull the appropriate titles. My column names are GumroadURL_Mac and GumroadURL_Win so the $MacWin variable is substituted into the SELECT statement.<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\n$qry = &quot;SELECT *\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM product, product_instance\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE product.id = product_id\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND GumroadURL_$MacWin IS NOT NULL\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY name&quot;;\n<\/code><\/pre><\/p>\n<p>I use a full ternary operator to get the right column from the row.<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nfor ($i = 0; $i &lt; $numRows; $i++) {\n&nbsp;&nbsp;&nbsp;&nbsp;$row&nbsp;&nbsp; = $res-&gt;fetch_array();\n&nbsp;&nbsp;&nbsp;&nbsp;$name = $row[&#039;name&#039;];\n&nbsp;&nbsp;&nbsp;&nbsp;$tagline = $row[&#039;tagline&#039;];\n&nbsp;&nbsp;&nbsp;&nbsp;$GumroadURL = ($MacWin == &#039;Mac&#039; ? $row[&#039;GumroadURL_Mac&#039;] :$row[&#039;GumroadURL_Win&#039;]);\n<\/code><\/pre><\/p>\n<p>As you can see, it makes the code much easier to read and in this example, I have one page of code that easily generates two web pages.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ternary operators are a concise way to write conditional statements that have two possible outcomes. Rather than writing a longer series of if\/then\/else statements you can write one line that makes it clear what the two choices are. In this example from iOS, I use a string for direction\u2014either clockwise or counterclockwise\u2014and translate it to &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1496\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Ternary operator<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1496","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1496"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1496\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}