{"id":3503,"date":"2022-11-21T09:37:46","date_gmt":"2022-11-21T09:37:46","guid":{"rendered":"https:\/\/info.documotor.com\/?page_id=3503"},"modified":"2023-01-25T10:18:43","modified_gmt":"2023-01-25T10:18:43","slug":"if","status":"publish","type":"page","link":"https:\/\/info.documotor.com\/?page_id=3503","title":{"rendered":"If"},"content":{"rendered":"\n<p>The if function can be used to output a specific output or other transformation, based on the evaluation of a boolean value. <\/p>\n\n\n\n<h3>Syntax<\/h3>\n\n\n\n<pre title=\"\" class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">any if(any $condition, any $trueValue, any $falseValue)<\/code><\/pre>\n\n\n\n<h3>Parameters<\/h3>\n\n\n\n<p><strong>$condition:<\/strong> The boolean value that should be evaluated upon. A boolean value always returns either <code>true<\/code> or <code>false<\/code>, based on the comparison operator. It evaluates as a <em><a href=\"https:\/\/info.documotor.com\/?page_id=644\" data-type=\"URL\" data-id=\"https:\/\/info.documotor.com\/?page_id=644\">truthy<\/a><\/em> whereas a non-empty string will evaluate as true. The condition can be set by the operators below: <\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">! <\/td><td class=\"has-text-align-left\" data-align=\"left\">NOT<\/td><td class=\"has-text-align-left\" data-align=\"left\">The not operator is used to check if a variable is not true. In other words, it checks boolean values of a statement. This could be e.g., to check if a variable exists or not.<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">!=<\/td><td class=\"has-text-align-left\" data-align=\"left\">Not Equal<\/td><td class=\"has-text-align-left\" data-align=\"left\">The not equal is usually used to check if a value is not equal (a != b)<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">&amp;&amp;<\/td><td class=\"has-text-align-left\" data-align=\"left\">AND<\/td><td class=\"has-text-align-left\" data-align=\"left\">If you want two or more variables to both be true, you use the &amp;&amp; operator<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">||<\/td><td class=\"has-text-align-left\" data-align=\"left\">OR<\/td><td class=\"has-text-align-left\" data-align=\"left\">Use this operator if you want just one of more values to be true <\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">&lt;<\/td><td class=\"has-text-align-left\" data-align=\"left\">Less than<\/td><td class=\"has-text-align-left\" data-align=\"left\">Check if one value is less than some other value<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">&gt;<\/td><td class=\"has-text-align-left\" data-align=\"left\">Greater than<\/td><td class=\"has-text-align-left\" data-align=\"left\">Check if one value is greater than another<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">==<\/td><td class=\"has-text-align-left\" data-align=\"left\">Equals<\/td><td class=\"has-text-align-left\" data-align=\"left\">Check if two values are equal to each other<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">&lt;=<\/td><td class=\"has-text-align-left\" data-align=\"left\">Less than or Equal<\/td><td class=\"has-text-align-left\" data-align=\"left\">Check if a value is less than or equal to each other<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">&gt;=<\/td><td class=\"has-text-align-left\" data-align=\"left\">Greater than or Equal<\/td><td class=\"has-text-align-left\" data-align=\"left\">Check if a value is greater than or equal to each other<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">?<\/td><td class=\"has-text-align-left\" data-align=\"left\">Filter Expression<\/td><td class=\"has-text-align-left\" data-align=\"left\">This is used to filter an array<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>$trueValue: <\/strong>The value that should be returned if $condition is true. Can also be a transformation. <\/p>\n\n\n\n<p><strong>$falseValue: <\/strong>The value that should be returned if $condition is false. Can also be a transformation.<\/p>\n\n\n\n<h2>Example<\/h2>\n\n\n\n<pre title=\"Data\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"a\": \"Test\",\n    \"b\": true,\n    \"c\": \"\",\n    \"d\": null,\n    \"e\": false\n}<\/code><\/pre>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript line-numbers\">{\n    alpha: if(a, 'Evaluates true', 'Evaluates false'),\n    beta: if(b, 'Evaluates true', 'Evaluates false'),\n    charlie: if(c, 'Evaluates true', 'Evaluates false'),\n    delta: if(d, 'Evaluates true', 'Evaluates false'),\n    echo: if(e, 'Evaluates true', 'Evaluates false'),\n    foxtrot: if(a == 'Test', 'Evaluates true', 'Evaluates false')\n}<\/code><\/pre>\n\n\n\n<pre title=\"Output\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"alpha\": \"Evaluates true\",\n    \"beta\": \"Evaluates true\",\n    \"charlie\": \"Evaluates false\",\n    \"delta\": \"Evaluates false\",\n    \"echo\": \"Evaluates false\",\n    \"foxtrot\": \"Evaluates true\"\n}<\/code><\/pre>\n\n\n\n<p>In this example, the values evaluate either true or false. Specifically, it returns either the string &#8220;Evaluates true&#8221; or &#8220;Evaluates false.&#8221; Note how the final transformation, &#8220;foxtrot,&#8221; makes a comparison, and asks whether or not &#8220;a&#8221; is the same as the value &#8220;Test.&#8221; Also, the empty string and null value make the if function evaluates as false, for example, &#8220;charlie&#8221; and &#8220;delta.&#8221; <\/p>\n\n\n\n<h3>Notes<\/h3>\n\n\n\n<p><strong>Error handling<\/strong><\/p>\n\n\n\n<p>The if function is useful for error handling a transformation. For example, if an input is expected to be an array, and some array-specific operations is supposed to be executed on the input, it would be beneficial  to encapsulate the operation with an if like <code>if(arr1 == to_array(arr1), to_array(arr1), split_array(arr1, <code>2<\/code>), '')<\/code>, where the false output would give an empty string, instead of an error. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The if function can be used to output a specific output or other transformation, based on the evaluation of a boolean value. Syntax Parameters $condition: The boolean value that should be evaluated upon. A boolean value always returns either true or false, based on the comparison operator. It evaluates as a truthy whereas a non-empty [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"parent":271,"menu_order":80,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3503"}],"collection":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3503"}],"version-history":[{"count":12,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3503\/revisions"}],"predecessor-version":[{"id":3906,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3503\/revisions\/3906"}],"up":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/271"}],"wp:attachment":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}