{"id":2846,"date":"2018-10-26T09:06:46","date_gmt":"2018-10-26T03:36:46","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=2846"},"modified":"2018-10-26T09:06:46","modified_gmt":"2018-10-26T03:36:46","slug":"php-if-else-statements","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/php-if-else-statements\/","title":{"rendered":"PHP :: If&#8230;Else Statements"},"content":{"rendered":"<p><strong>PHP :: If&#8230;Else Statements<\/p>\n<p>Date Posted<\/strong> : <strong>25-Oct-2018<\/strong><\/p>\n<p>Conditional statements are used to perform different actions based on different conditions.<br \/>\nConditional Statements<br \/>\nVery often when you write code, you want to perform different actions for different decisions.<br \/>\nYou can use conditional statements in your code to do this.<br \/>\nIn PHP we have the following conditional statements:<\/p>\n<p>if statement &#8211; use this statement to execute some code only if a specified condition is true<br \/>\nif&#8230;else statement &#8211; use this statement to execute some code if a condition is true and another code if the condition is false<br \/>\nif&#8230;elseif&#8230;.else statement &#8211; use this statement to select one of several blocks of code to be executed<\/p>\n<p><strong>The if Statement:<\/strong><br \/>\nUse the if statement to execute some code only if a specified condition is true.<br \/>\n<strong>Syntax<\/strong><br \/>\nif (condition) code to be executed if condition is true;<br \/>\nThe following example will output &#8220;Have a nice weekend!&#8221; if the current day is Friday:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n&lt;body&gt;\r\n\r\n&lt;?php\r\n$d=date(\"D\");\r\nif ($d==\"Fri\") echo \"Have a nice weekend!\";\r\n?&gt;<\/pre>\n<p><strong>The if&#8230;else Statement<\/strong>:<br \/>\nUse the if&#8230;.else statement to execute some code if a condition is true and another code if a condition is false.<br \/>\nSyntax<br \/>\nif (condition)<br \/>\ncode to be executed if condition is true;<br \/>\nelse<br \/>\ncode to be executed if condition is false;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\nNotice that there is no ..else.. in this syntax. The code is executed only if the specified condition is true.<\/p>\n<p><strong>Example<\/strong><br \/>\nThe following example will output &#8220;Have a nice weekend!&#8221; if the current day is Friday, otherwise it will output &#8220;Have a nice day!&#8221;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n&lt;body&gt;\r\n\r\n&lt;?php\r\n$d=date(\"D\");\r\nif ($d==\"Fri\")\r\n  echo \"Have a nice weekend!\";\r\nelse\r\n  echo \"Have a nice day!\";\r\n?&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>If more than one line should be executed if a condition is true\/false, the lines should be enclosed within curly braces:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n&lt;body&gt;\r\n\r\n&lt;?php\r\n$d=date(\"D\");\r\nif ($d==\"Fri\")\r\n  {\r\n  echo \"Hello!&lt;br \/&gt;\";\r\n  echo \"Have a nice weekend!\";\r\n  echo \"See you on Monday!\";\r\n  }\r\n?&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>The if&#8230;elseif&#8230;.else Statement:<\/strong>Use the if&#8230;.elseif&#8230;else statement to select one of several blocks of code to be executed.<br \/>\nSyntax<br \/>\nif (condition)<br \/>\ncode to be executed if condition is true;<br \/>\nelseif (condition)<br \/>\ncode to be executed if condition is true;else<br \/>\ncode to be executed if condition is false;<br \/>\n<strong>Example<\/strong><br \/>\nThe following example will output &#8220;Have a nice weekend!&#8221; if the current day is Friday, and &#8220;Have a nice Sunday!&#8221; if the current day is Sunday. Otherwise it will output &#8220;Have a nice day!&#8221;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n&lt;body&gt;\r\n\r\n&lt;?php\r\n$d=date(\"D\");\r\nif ($d==\"Fri\")\r\n  echo \"Have a nice weekend!\";\r\nelseif ($d==\"Sun\")\r\n  echo \"Have a nice Sunday!\";\r\nelse\r\n  echo \"Have a nice day!\";\r\n?&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP :: If&#8230;Else Statements Date Posted : 25-Oct-2018 Conditional statements are used to perform different actions based on different conditions. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[273],"class_list":["post-2846","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-php","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-JU","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2846"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2846\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}