{"id":15569,"date":"2020-06-29T18:40:41","date_gmt":"2020-06-29T18:40:41","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=15569"},"modified":"2022-04-28T09:15:23","modified_gmt":"2022-04-28T09:15:23","slug":"php-7-fundamental-tutorial-for-beginners-php-switchcase-statements","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/php-7-fundamental-tutorial-for-beginners-php-switchcase-statements\/","title":{"rendered":"PHP 7 Fundamental Tutorial for Beginners &#8211; PHP Switch\u2026Case Statements?"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">What is PHP&nbsp;Switch\u2026Case&nbsp;Statements<\/h3>\n\n\n\n<p>Check Several Possible constant values for an expression.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">PHP If\u2026Else Vs Switch\u2026Case<\/h4>\n\n\n\n<p>The switch-case statement is an alternative to the if-elseif-else statement, which does almost the same thing. The switch-case statement tests a variable against a series of values until it finds a match, and then executes the block of code corresponding to that match.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Syntax:-<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/Swith.png\" alt=\"\" class=\"wp-image-15570\"\/><\/figure>\n\n\n\n<p><strong>Consider the following example, which displays a different message for each day.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/swith1.png\" alt=\"\" class=\"wp-image-15571\"\/><\/figure>\n\n\n\n<p><strong>Example<\/strong>:-<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/d7491736dd311a504ec3e0768923944c.js\"><\/script>\n\n\n\n<p>The&nbsp;<code>switch-case<\/code>&nbsp;statement differs from the&nbsp;<code>if-elseif-else<\/code>&nbsp;statement in one important way. The&nbsp;<code>switch<\/code>&nbsp;statement executes line by line (i.e. statement by statement) and once PHP finds a&nbsp;<code>case<\/code>&nbsp;statement that evaluates to true, it&#8217;s not only executes the code corresponding to that case statement, but also executes all the subsequent&nbsp;<code>case<\/code>&nbsp;statements till the end of the&nbsp;<code>switch<\/code>&nbsp;block automatically.<\/p>\n\n\n\n<p><strong>This Example for Without Break<\/strong>:-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/2-2.png\" alt=\"\" class=\"wp-image-15572\"\/><\/figure>\n\n\n\n<p><strong>This Example for different case to same Statements <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/4-1.png\" alt=\"\" class=\"wp-image-15573\"\/><\/figure>\n\n\n\n<p><strong>This Example for one case two Statements<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/1-4.png\" alt=\"\" class=\"wp-image-15574\"\/><\/figure>\n\n\n\n<p><strong>This Example For variable not match so print is default<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/ef20c6b136aed839bc4a2352fe2fd672.js\"><\/script>\n\n\n\n<p>To prevent this add a&nbsp;<code>break<\/code>&nbsp;statement to the end of each&nbsp;<code>case<\/code>&nbsp;block. The&nbsp;<code>break<\/code>&nbsp;statement tells PHP to break out of the&nbsp;<code>switch-case<\/code>&nbsp;statement block once it executes the code associated with the first true case.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/default.png\" alt=\"\" class=\"wp-image-15577\"\/><\/figure>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_84002\"><iframe loading=\"lazy\"  id=\"_ytid_94591\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_84002\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>What is PHP&nbsp;Switch\u2026Case&nbsp;Statements Check Several Possible constant values for an expression. PHP If\u2026Else Vs Switch\u2026Case The switch-case statement is an alternative to the if-elseif-else statement, which does almost the same&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5150],"tags":[4388,177,6195,3131],"class_list":["post-15569","post","type-post","status-publish","format-standard","hentry","category-php","tag-case","tag-php","tag-statements","tag-switch"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=15569"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15569\/revisions"}],"predecessor-version":[{"id":24703,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15569\/revisions\/24703"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=15569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=15569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=15569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}