{"id":1402,"date":"2018-01-19T19:22:48","date_gmt":"2018-01-19T19:22:48","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/the-ternary-conditional-operator\/"},"modified":"2018-01-19T19:24:45","modified_gmt":"2018-01-19T19:24:45","slug":"the-ternary-conditional-operator","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/the-ternary-conditional-operator\/","title":{"rendered":"The Ternary Conditional Operator"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By PHP Builder Staff<\/div>\n<div class=\"\">on March 6, 2008<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\"><i>submitted by scross<\/i><\/p>\n<div class=\"articlePara\">\nChecking for variables before you use them can be a tedious process, and this step is often missed out in PHP code, leading to masses of PHP Notice errors and possibly leaving the application vulnerable. However, there is a simple solution to this problem, something called the ternary conditional operator. This allows you to check for the existence of a variable (or check that the variable has a valid value) and assign a value accordingly. This is very useful when you are dealing with $_GET, $_POST, $_SESSION etc. variables, because you don&#8217;t know whether the incoming variable will exist, and if it doesn&#8217;t you might want to assign a default value. Here is the format of the ternary conditional operator:\n    <\/div>\n<div class=\"articleExample\">\n<pre>\n<code>\nCONDITION ? VALUE IF TRUE : VALUE IF FALSE\n<\/code>\n<\/pre>\n<\/p><\/div>\n<div class=\"articlePara\">\nHere is an example to hopefully put this into context:\n    <\/div>\n<div class=\"articleExample\">\n<pre><code>\n&lt;?php\n$id = isset($_GET['id']) ? $_GET['id'] : false;\n?&gt;\n<\/code><\/pre>\n<\/div>\n<div class=\"articlePara\">\nThis one line of code does a surprisingly large amount. Firstly, it uses the isset() function to check if $_GET[&#8216;id&#8217;] exists. If $_GET[&#8216;id&#8217;] does exist it simply returns its value. However, if it does not exist the operator returns false. The value that the operator returns is then assigned to the variable $id. So, basically, if $_GET[&#8216;id&#8217;] exists then $id = $_GET[&#8216;id&#8217;], however if it does not exist then $id = false\n    <\/div>\n<div class=\"articlePara\">\nThe operator can be useful in a number of applications, and helps you to avoid loads of unnecessary if statements.\n    <\/div>\n<p><\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Checking for variables before you use them can be a tedious process, and this step is often missing in PHP code, leading to errors and potential<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1402","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1402"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1402\/revisions"}],"predecessor-version":[{"id":3269,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1402\/revisions\/3269"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}