{"id":237,"date":"2018-07-15T02:34:02","date_gmt":"2018-07-15T02:34:02","guid":{"rendered":"https:\/\/www.go4expert.com\/tutorials\/?p=237"},"modified":"2018-08-02T02:40:53","modified_gmt":"2018-08-02T02:40:53","slug":"php-decision-making","status":"publish","type":"post","link":"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/","title":{"rendered":"PHP Decision Making"},"content":{"rendered":"<p>Decision making statements are essential in programming because they help us control the flow of execution in a program. The outcome of the condition which is specified in the decision-making statement will determine the lines of code which will be executed in the program.<\/p>\n<p>Different actions can be taken depending on whether the condition evaluates to true or false. Because of this\u00a0decision making statements are also known as conditional statements.<\/p>\n<p>The following are the conditional statements available in PHP.<\/p>\n<ol>\n<li>if statement \u2013 This executes a particular set of statements only if the condition evaluates to true.<\/li>\n<li>if..else statement \u2013 executes a set of statements if a condition is true and another set of statements if the condition is false<\/li>\n<li>if.. elseif ... else statement \u2013 Multiple conditions are evaluated and different statement blocks get executed depending on which of the conditions is true.<\/li>\n<li>switch statement \u2013selects one of the many blocks of code to be executed depending on the value of one condition.<\/li>\n<\/ol>\n<h2>IF Statement in PHP<\/h2>\n<p>The If.. condition statement allows us to execute a selected set of statements if a condition is true. If the condition is false the set of statements will be skipped during program execution.<\/p>\n<p><b>Syntax :<\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">if(condition)\r\n{\r\n\u00a0 \u00a0 Statements which will be executed because the condition is true;\r\n}<\/textarea><\/div><\/pre>\n<p><b>Example :<\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\r\n$a =20;\r\nIf($a %2 ==0)\r\n\u00a0 \u00a0 echo \" Even Number because $a%2==0 evaluates to true\";\r\n\/\/ the output will be \u201cEven Number\u201d because the condition is true.\r\nIf($a %2 !=0)\r\n\u00a0 \u00a0 echo \" Odd Number\";\r\n\/\/The above statement will not be executed because the corresponding If statement evaluates to false.\r\n?&gt;<\/textarea><\/div><\/pre>\n<h2>IF\u2026ELSE Statement in PHP<\/h2>\n<p>The If......Else statement is used if we want to execute a set of statements when a condition is true and execute another set of statements if a condition is false.<\/p>\n<p><b>Syntax :<\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">if(condition)\r\n{\r\n\u00a0 \u00a0 Statements are executed because condition is true;\r\n}\r\nelse\r\n{\r\n\u00a0 \u00a0 Statements are executed because condition is false;\r\n}<\/textarea><\/div><\/pre>\n<p><b>Example :<\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\r\n$a =20; $B=30;\r\nIf($a &gt; $b)\r\n\u00a0 \u00a0 echo \u201ca is Bigger than b because $a&gt;$b is true\u201d;\r\nelse\r\n\u00a0 \u00a0 echo \u201cb is Bigger than a because $a&gt;$b is false\u201d;\r\n\/\/ The output will be \u201cb is Bigger than a\u201d because the condition is false and the \u2018Else\u2019 block of statements will be executed.\r\n?&gt;<\/textarea><\/div><\/pre>\n<h2>IF\u2026ELSEIF\u2026ELSE Statement in PHP<\/h2>\n<p>The IF\u2026ELSEIF\u2026ELSE statement is used because we want to execute one of the multiple sets of statements while testing more than one condition.<\/p>\n<p>There could any number of conditions which could be evaluated and any number of sets of statements which could be executed because of the outcome of the conditions.<\/p>\n<p><b>Syntax :<\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">if(condition)\r\n{\r\n\u00a0 \u00a0 Statements which are to be executed because the condition is true;\r\n}\r\nelseif\r\n{\r\n\u00a0 \u00a0 Statements which are to be executed because the condition is true;\r\n}\r\nelse\r\n{\r\n\u00a0 \u00a0 Statements which are to be executed because the condition is false;\r\n}<\/textarea><\/div><\/pre>\n<p><b>Example :<\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\r\n$a =18; $b=12;\r\nIf($a == $b)\r\n\u00a0 \u00a0 echo \u201ca and b are Equal\u201d;\r\nelseif ($a &gt; $b)\r\n\u00a0 \u00a0 echo \u201ca is Bigger than b because $a&gt;$b is true and $a==$b is false\u201d;\r\nelse\r\n\u00a0 \u00a0 echo \u201ca is Bigger than b because $a&gt;$b is false and $a==$b is false\u201d;\r\n\/\/ the program can have three different outputs because of the 2 conditions which are specified.\r\n?&gt;<\/textarea><\/div><\/pre>\n<p>The output will be :<\/p>\n<pre>a is Bigger than b because $a&gt;$b is true and $a==$b is false<\/pre>\n<h2>Switch Statement in PHP<\/h2>\n<p>The switch statement is used to compare the same condition or expression with different values and execute the different set of statements because of its value match.<\/p>\n<p>It is similar to a series of IF statements because it checks for the value of the same expression. However, it checks for the match of the exact value with the case labels. The switch statement is generally used because we can avoid long blocks of 'if..elseif..else' code.<\/p>\n<p><b>Syntax :<\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">switch (expression)\r\n{\r\nCase label 1 :\r\n\u00a0 \u00a0 \/\/Code to be executed because expression = label 1;\r\n\u00a0 \u00a0 break;\r\nCase label 2 :\r\n\u00a0 \u00a0 \/\/Code to be executed because expression = label 2;\r\n\u00a0 \u00a0 break;\r\n\r\n....\r\ndefault :\r\n\u00a0 \u00a0 Default code to be executed because none of the labels matched with the expression;\r\n\u00a0 \u00a0 break;\r\n}<\/textarea><\/div><\/pre>\n<p><b>Example :<\/b><\/p>\n<p>The code evaluates the given expression and finds a label to match the resulting value. If the matching value of the label is found then the code associated with the matching label will be executed. The code in the default case will be executed when there is no match.<\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;html&gt;\r\n&lt;body&gt;\r\n\r\n&lt;?php\r\n$d = \"Mon\";\r\n\r\nswitch ($d){\r\ncase \"Mon\":\r\n\u00a0 \u00a0 echo \"Today is Monday because $d is 'Mon'\";\r\n\u00a0 \u00a0 break;\r\n\r\ncase \"Tue\":\r\n\u00a0 \u00a0 echo \"Today is Tuesday because $d is 'Tue'\";\r\n\u00a0 \u00a0 break;\r\n\r\ncase \"Wed\":\r\n\u00a0 \u00a0 echo \"Today is Wednesday because $d is 'Wed'\";\r\n\u00a0 \u00a0 break;\r\n\r\ncase \"Thu\":\r\n\u00a0 \u00a0 echo \"Today is Thursday because $d is 'Thu'\";\r\n\u00a0 \u00a0 break;\r\n\r\ncase \"Fri\":\r\n\u00a0 \u00a0 echo \"Today is Friday because $d is 'Fri'\";\r\n\u00a0 \u00a0 break;\r\n\r\ncase \"Sat\":\r\n\u00a0 \u00a0 echo \"Today is Saturday because $d is 'Sat'\";\r\n\u00a0 \u00a0 break;\r\n\r\ncase \"Sun\":\r\n\u00a0 \u00a0 echo \"Today is Sunday because $d is 'Sun'\";\r\n\u00a0 \u00a0 break;\r\n\r\ndefault:\r\n\u00a0 \u00a0 echo \"Wonder which day is this because $d did not match with any case label ?\";\r\n}\r\n?&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/textarea><\/div><\/pre>\n<p>The output will be \"Today is Monday\u00a0 because $d is 'Mon'\"<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Decision-making statements play a crucial role in controlling the flow of execution . Learn in detail about the different decision making statements.<\/p>\n","protected":false},"author":6,"featured_media":210,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[7],"tags":[],"class_list":{"0":"post-237","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-php","8":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Decision Making - Go4Expert Tutorials<\/title>\n<meta name=\"description\" content=\"Decision-making statements play a crucial role in controlling the flow of execution . Learn in detail about the different decision making statements.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Decision Making - Go4Expert Tutorials\" \/>\n<meta property=\"og:description\" content=\"Decision-making statements play a crucial role in controlling the flow of execution . Learn in detail about the different decision making statements.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/\" \/>\n<meta property=\"og:site_name\" content=\"Go4Expert Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-15T02:34:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-08-02T02:40:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"770\" \/>\n\t<meta property=\"og:image:height\" content=\"385\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"vnlaks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"vnlaks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/\",\"name\":\"PHP Decision Making - Go4Expert Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\"},\"datePublished\":\"2018-07-15T02:34:02+00:00\",\"dateModified\":\"2018-08-02T02:40:53+00:00\",\"author\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae\"},\"description\":\"Decision-making statements play a crucial role in controlling the flow of execution . Learn in detail about the different decision making statements.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.go4expert.com\/tutorials\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP Decision Making\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/\",\"name\":\"Go4Expert Tutorials\",\"description\":\"Programming And Web Development Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.go4expert.com\/tutorials\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae\",\"name\":\"vnlaks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g\",\"caption\":\"vnlaks\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Decision Making - Go4Expert Tutorials","description":"Decision-making statements play a crucial role in controlling the flow of execution . Learn in detail about the different decision making statements.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/","og_locale":"en_US","og_type":"article","og_title":"PHP Decision Making - Go4Expert Tutorials","og_description":"Decision-making statements play a crucial role in controlling the flow of execution . Learn in detail about the different decision making statements.","og_url":"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/","og_site_name":"Go4Expert Tutorials","article_published_time":"2018-07-15T02:34:02+00:00","article_modified_time":"2018-08-02T02:40:53+00:00","og_image":[{"width":770,"height":385,"url":"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg","type":"image\/jpeg"}],"author":"vnlaks","twitter_card":"summary_large_image","twitter_misc":{"Written by":"vnlaks","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/","url":"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/","name":"PHP Decision Making - Go4Expert Tutorials","isPartOf":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#website"},"datePublished":"2018-07-15T02:34:02+00:00","dateModified":"2018-08-02T02:40:53+00:00","author":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae"},"description":"Decision-making statements play a crucial role in controlling the flow of execution . Learn in detail about the different decision making statements.","breadcrumb":{"@id":"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.go4expert.com\/tutorials\/php-decision-making\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.go4expert.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"PHP Decision Making"}]},{"@type":"WebSite","@id":"https:\/\/www.go4expert.com\/tutorials\/#website","url":"https:\/\/www.go4expert.com\/tutorials\/","name":"Go4Expert Tutorials","description":"Programming And Web Development Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.go4expert.com\/tutorials\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae","name":"vnlaks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g","caption":"vnlaks"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9JSSi-3P","_links":{"self":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/comments?post=237"}],"version-history":[{"count":12,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/237\/revisions"}],"predecessor-version":[{"id":426,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/237\/revisions\/426"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media\/210"}],"wp:attachment":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media?parent=237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/categories?post=237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/tags?post=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}