{"id":689,"date":"2017-02-20T14:51:21","date_gmt":"2017-02-20T14:51:21","guid":{"rendered":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/?page_id=689"},"modified":"2023-10-22T10:04:26","modified_gmt":"2023-10-22T10:04:26","slug":"php-programming-language-basics-conditional-statements-if-elseif-else","status":"publish","type":"page","link":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-4-adding-a-dynamic-layer-introducing-the-php-programming-language\/php-programming-language-basics-conditional-statements-if-elseif-else\/","title":{"rendered":"4-5: PHP programming language basics &#8211; conditional statements &#8211; if, elseif, else"},"content":{"rendered":"<p>In the flow of an application it is a common situation that some portions of code should be executed only if particular conditions are met. We can exert this control by using one or more conditional statements.<\/p>\n<h2>The if statement<\/h2>\n<p>Depending on the situation, an &#8220;if&#8221; statement could be all we need: if a particular situation is met, we will execute some special code, otherwise we just do nothing special and continue executing the code that follows the &#8220;if&#8221; statement.<\/p>\n<p>In the following example we see how to implement a set a banned IP addresses that will be unable to see the contents of a particular web page, based on a simple if statement.<\/p>\n<pre lang=\"php\"><code>\n<?php\n\n$header = file_get_contents(\"html\/header.html\");\n$footer = file_get_contents(\"html\/footer.html\");\n$page_contents = \"\nHello, world!\n\";\n$denied_contents = \"\nSorry, you are not allowed to see the contents of this page\n\";\n\n$visitor_ip = $_SERVER['REMOTE_ADDR']; \/\/ We store the visitor IP to the $visitor_ip variable\n\n\/\/ We have a list of IP addresses that should not access this web page contents\n$banned_ips = array(\"160.80.22.35\",\"160.110.22.36\",\"160.110.22.37\",\"160.110.22.38\");\n\n\/\/ We now check if the visitor's IP is among the banned IP addresses\nforeach($banned_ips as $banned_ip){\n    if($visitor_ip == $banned_ip){\n        \/\/ if we get a match we output a special page and exit the PHP script with the \"die\" function\n        die($header.$denied_contents.$footer);\n    }\n}\n\n\/\/ The following will be executed only if we did not find a match\n\/\/ between the visitor's IP and one of the banned IP addresses\n\necho $header.$page_contents.$footer;\n\n?>\n<\/code><\/pre>\n<p>This example is worth a few comments:<\/p>\n<ul>\n<li> In contrast to the equal to sign =, which is an &#8220;assignment operator&#8221;, the double equal to sign == is a PHP &#8220;comparison operator&#8221;. It will return &#8220;true&#8221; if the elements on each side are equal and &#8220;false&#8221; if they are not. Comparison operators are used often within conditional statements to check if a certain condition is met. We will discuss them in more detail below.<\/li>\n<li> The die() function is used to print a message, passed to the function as argument, and terminate the execution of a PHP script.<\/li>\n<li> This is a nice example of a dynamic page whose contents change depending on a particular condition (in this case the visitor&#8217;s IP address)<\/li>\n<\/ul>\n<h2>The elseif and else statements<\/h2>\n<p>If we need to check for two or more precisely defined alternative conditions, we can use an if statement followed by one or more elseif statements.<\/p>\n<p>if there is a code we wish to execute when neither the if condition nor any of the alternative elseif conditions are met, we can also add an (optional) else statement at the end.<\/p>\n<p>In other cases we can manage our conditions by using an if directly followed by an else, omitting the elseif. It really depends on how how conditions are structured and should be applied within our code.<\/p>\n<p>In the following example we see how to show different contents to visitors from different IP addresses in a webpage based on an set of if, elseif and else statements.<\/p>\n<pre lang=\"php\"><code>\n<?php\n\n$header = file_get_contents(\"html\/header.html\");\n$footer = file_get_contents(\"html\/footer.html\");\n\n\/\/ For everyone, but the banned IP, to see \n$page_contents = \"\nHello, world!\n\\n\";\n\n\/\/ Just for Brian, a friend with a known IP address (160.110.22.130)\n$brian_message = \"\nHey Brian, the key is under to stone to the left side of the door\n\\n\"; \n\n\/\/ Message to show to the banned IP \n$denied_contents = \"\nSorry, you are not allowed to see the contents of this page\n\\n\";\n\n$visitor_ip = $_SERVER['REMOTE_ADDR']; \/\/ We store the visitor IP to the $visitor_ip variable\n\n$banned_ip = \"160.110.22.35\"; \/\/ We have just one banned IP this time\n\n$brian_ip = \"160.110.22.130\"; \/\/ Brian's IP address\n\n\/\/ We now issue different pages depending on the visitor's IP address\nif($visitor_ip == $banned_ip){\n    \/\/ if the IP is the banned one we output a special page \n    echo $header.$denied_contents.$footer;\n}\nelseif($visitor_ip == $brian_ip){\n    \/\/ Just for brian\n    echo $header.$page_contents.$brian_message.$footer;\n}\nelse{\n    \/\/ For everyone else\n   echo $header.$page_contents.$footer;\n}\n\n?>\n<\/code><\/pre>\n<h2>PHP comparison operators<\/h2>\n<p>Within conditional statements, whether the condition to be met is true or false is often checked by using comparison operators. In the previous example we used ==, however there are several others.<\/p>\n<h3>The == equality operator<\/h3>\n<p>The == equality operator checks if the value of two elements is the same. This is however different from checking if they are identical, that is if they have the same value and also the same type.<\/p>\n<p>For example:<\/p>\n<pre lang=\"php\"><code>\n<?php\n\n$a = 1; \/\/ Variable type is an integer\n$b = 1;\n$c = \"1\"; \/\/ Variable type is a string\n\nif($a == $b){ \/\/ This is of course true in this case\n    echo \"\na equals b\n\\n\";\n}\n\nif($a == $c){ \/\/ This is also true despite that the variable type of $a and $c is different\n    echo \"\na equals c\n\\n\";\n}\n\nif(false == 0){ \/\/ This is true, false and 0 are of different type (boolean and integer), but they both evaluate to false\n    echo \"\nthe two elements are equal\n\\n\";\n}\n\nif(13 == true){ \/\/ This is also true, 13 is not false, it is therefore true\n    echo \"\nthe two elements are equal\n\\n\";\n}\n\nif(false == 1){ \/\/ This is false\n    echo \"\nthe two elements are equal\n\\n\";\n}\n\nif(0 == true){ \/\/ This is false\n    echo \"\nthe two elements are equal\n\\n\";\n}\n\n?>\n<\/code><\/pre>\n<p>To check if two elements are NOT equal, we can use the <strong>&#8220;not equal to&#8221; operator !=<\/strong><\/p>\n<pre lang=\"php\"><code>\n<?php\n\n$a = 1;\n$b = 2;\n\nif($a != $b){\n    echo \"\na is not equal to b\n\";\n{\n\n?>\n<\/code><\/pre>\n<h3>The === identity operator<\/h3>\n<p>The triple equal to sign ===, also known as identity operator, checks if the value AND type of two variables are the same. That is, if they are identical. Again, in the realm of comparison operators, equality and identity are subtly distinct concepts. === is more restrictive than == as it checks both for value and type.<\/p>\n<pre lang=\"php\"><code>\n<?php\n\n$a = 1; \/\/ Variable type is an integer\n$b = 1;\n$c = \"1\"; \/\/ Variable type is a string\n\nif($a === $b){ \/\/ This is of course true in this case\n    echo \"\na identical b\n\\n\";\n}\n\nif($a === $c){ \/\/ False as the type of $a and $c is different, although the value is the same\n    echo \"\na equals c\n\\n\";\n}\n\nif(false === 0){ \/\/ False as the variable types are different\n    echo \"\nthe two elements are equal\n\\n\";\n}\n\nif(13 === true){ \/\/ False as the variable types are different\n    echo \"\nthe two elements are equal\n\\n\";\n}\n\n?>\n<\/code><\/pre>\n<p>To check if two elements are NOT identical, we can use the <strong>&#8220;not identical to&#8221; operator !==<\/strong><\/p>\n<pre lang=\"php\"><code>\n<?php\n\n$a = 1;\n$b = \"1\";\n\nif($a !== $b){\n    echo \"\na is not identical to b\n\";\n{\n\n?>\n<\/code><\/pre>\n<h3>>, <, >= and <= operators<\/h3>\n<p>Those are typically used to compare numbers for which they do quite what you would expect. They can also be used to compare strings, on the basis of both length and alphabetical criteria, however we will not discuss this here.<\/p>\n<ul>\n<li><strong>><\/strong> Bigger than<\/li>\n<li><strong><<\/strong> Smaller than<\/li>\n<li><strong>>=<\/strong> Bigger or equal<\/li>\n<li><strong><=<\/strong> Smaller or equal<\/li>\n<\/ul>\n<pre lang=\"php\"><code>\n<?php\n\n$a = 1;\n$b = 2;\n\nif($a > $b){\n    echo \"\na is bigger than b\n\";\n{\nelseif($a < $b){\n    echo \"\na is smaller than b\n\";\n}\nelse{\n    echo \"\nif a and b are numbers, likely a equals to b at this point\n\";\n}\n\n?>\n<\/code><\/pre>\n<p>For an exhaustive survey of all PHP operators, including comparison operators, we suggest you look at <a href=\"https:\/\/www.w3schools.com\/php\/php_operators.asp\" target=\"_blank\" rel=\"noopener\">this excellent page on the W3Schools website<\/a> as a reference.<\/p>\n<div class=\"google-ad\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- bioinfo web dev 2 --><br \/>\n<ins class=\"adsbygoogle\" style=\"display: inline-block; width: 728px; height: 90px;\" data-ad-client=\"ca-pub-0159360445983090\" data-ad-slot=\"3442176918\"><\/ins><br \/>\n<script><br \/>\n(adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/div>\n<h2>Chapter Sections<\/h2>\n<p>[pagelist include=&#8221;435&#8243;]<\/p>\n<p>[siblings]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the flow of an application it is a common situation that some portions of code should be executed only if particular conditions are met. We can exert this control by using one or more conditional statements. The if statement Depending on the situation, an &#8220;if&#8221; statement could be all we need: if a particular &hellip; <a href=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-4-adding-a-dynamic-layer-introducing-the-php-programming-language\/php-programming-language-basics-conditional-statements-if-elseif-else\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;4-5: PHP programming language basics &#8211; conditional statements &#8211; if, elseif, else&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":435,"menu_order":5,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-689","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/comments?post=689"}],"version-history":[{"count":44,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/689\/revisions"}],"predecessor-version":[{"id":3030,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/689\/revisions\/3030"}],"up":[{"embeddable":true,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/435"}],"wp:attachment":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/media?parent=689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}