{"id":5094,"date":"2025-11-28T17:19:57","date_gmt":"2025-11-28T16:19:57","guid":{"rendered":"https:\/\/sutilweb.eu\/?page_id=5094"},"modified":"2025-11-28T17:19:57","modified_gmt":"2025-11-28T16:19:57","slug":"09-estructura-de-seleccion-multiple-con-switch-o-match","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/php\/php-practico\/09-estructura-de-seleccion-multiple-con-switch-o-match\/","title":{"rendered":"09. Estructura de selecci\u00f3n m\u00faltiple con switch o match"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Este tipo de estructuras las vamos a utilizar cuando queramos comparar el valor de alguna variable y no queramos utilizar las condicionales anidadas.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bucle switch<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Veamos como funciona el bucle <strong><em>switch<\/em><\/strong> con un ejm.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$a = 5;\n\nswitch ($a) {\n    case '1':\n        echo \"Lunes\";\n        break;\n\n    case '2':\n        echo \"Martes\";\n        break;\n\n    case '3':\n      echo \"Mi\u00e9rcoles\";\n      break;\n\n    case '4':\n      echo \"Jueves\";\n      break;\n\n    case '5':\n      echo \"Viernes\";\n      break;\n\n    case '6':\n      echo \"S\u00e1bado\";\n      break;\n\n    case '7':\n      echo \"Domingo\";\n      break;      \n\n    default:\n      echo \"D\u00eda equivocado\";\n      break;\n  }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Como vemos, vamos utilizando la palabra <strong><em>case<\/em><\/strong> para cada caso que queremos que se cumpla, y utilizaremos la palabra <strong><em>default<\/em><\/strong> en el caso de que no se cumpla ninguno de los casos anteriores.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bucle match<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">La estructura <strong><em>match<\/em><\/strong> es una estructura nueva que viene de la mano de PHP 8 y nos sirve para lo mismo que la estructura&nbsp;<strong><em>switch<\/em><\/strong> pero m\u00e1s legible. Vamos a ver un ejm del uso de esta estructura.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n $a = 5;\n\n \/\/ Estructura match\n\n  echo match ($a) {\n     1 => 'Lunes',\n     2 => 'Martes',\n     3 => 'Mi\u00e9rcoles',\n     4 => 'Jueves',\n     5 => 'Viernes',\n     6 => 'S\u00e1bado',\n     7 => 'Domingo',\n     default => 'El valor de la variable a no es un valor v\u00e1lido'\n  };<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Este tipo de estructuras las vamos a utilizar cuando queramos comparar el valor de alguna variable y no queramos utilizar [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4518,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","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-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":"","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-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":"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":""},"mobile":{"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":""}},"footnotes":""},"class_list":["post-5094","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"Este tipo de estructuras las vamos a utilizar cuando queramos comparar el valor de alguna variable y no queramos utilizar [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/5094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=5094"}],"version-history":[{"count":4,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/5094\/revisions"}],"predecessor-version":[{"id":5098,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/5094\/revisions\/5098"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/4518"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}