
{"id":8191,"date":"2023-08-16T14:03:55","date_gmt":"2023-08-16T07:03:55","guid":{"rendered":"https:\/\/tapchicntt.com\/?p=8191"},"modified":"2023-08-22T08:24:39","modified_gmt":"2023-08-22T01:24:39","slug":"php-bai-5-cau-lenh-if-else-trong-php","status":"publish","type":"post","link":"https:\/\/tapchicntt.com\/php-bai-5-cau-lenh-if-else-trong-php\/","title":{"rendered":"[php] B\u00e0i 5: C\u00e2u l\u1ec7nh if else trong php"},"content":{"rendered":"\n<p>C\u00e1c v\u1ea5n \u0111\u1ec1 ta \u0111\u1ec1 c\u1eadp t\u1eeb \u0111\u1ea7u b\u00e0i cho \u0111\u1ebfn nay ch\u1ec9 cho ph\u00e9p ch\u00fang ta vi\u1ebft ch\u01b0\u01a1ng tr\u00ecnh ch\u1ea1y theo m\u1ed9t tr\u00ecnh t\u1ef1 t\u1eeb tr\u00ean xu\u1ed1ng v\u00e0 kh\u00f4ng b\u1ecf qua m\u1ed9t \u0111o\u1ea1n code n\u00e0o (<em>tr\u1eeb ph\u1ea7n ghi ch\u00fa<\/em>).<\/p>\n\n\n\n<p>Tuy nhi\u00ean trong th\u1ef1c t\u1ebf kh\u00f4ng ph\u1ea3i l\u00fac n\u00e0o ta c\u0169ng mu\u1ed1n ch\u01b0\u01a1ng tr\u00ecnh ch\u1ea1y h\u1ebft c\u00e1c d\u00f2ng l\u1ec7nh \u0111\u00f3. V\u00ec th\u1ebf&nbsp;<strong>c\u00e2u l\u1ec7nh if else trong php<\/strong>&nbsp;ra \u0111\u1eddi gi\u00fap ta gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1 n\u00e0y.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. C\u00e2u l\u1ec7nh \u0111i\u1ec1u ki\u1ec7n l\u00e0 g\u00ec ?<\/h3>\n\n\n\n<p><strong>C\u00e2u l\u1ec7nh \u0111i\u1ec1u ki\u1ec7n if else<\/strong>&nbsp;cho ph\u00e9p ta thay \u0111\u1ed5i lu\u1ed3ng c\u1ee7a ch\u01b0\u01a1ng tr\u00ecnh d\u1ef1a tr\u00ean m\u1ed9t \u0111i\u1ec1u ki\u1ec7n n\u00e0o \u0111\u00f3.<\/p>\n\n\n\n<p>N\u1ebfu \u0111i\u1ec1u ki\u1ec7n l\u00e0 \u0111\u00fang (true) th\u00ec ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n, ng\u01b0\u1ee3c l\u1ea1i n\u1ebfu \u0111i\u1ec1u ki\u1ec7n \u0111\u01b0a ra l\u00e0 sai (false) th\u00ec n\u1ed9i dung c\u00f4ng vi\u1ec7c \u0111\u00f3 s\u1ebd kh\u00f4ng \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n.<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5:<\/strong>&nbsp;\u0111\u1ec3 ki\u1ec3m tra m\u1ed9t s\u1ed1 l\u00e0 s\u1ed1 ch\u1eb5n hay s\u1ed1 l\u1ebb ta th\u1ef1c hi\u1ec7n c\u00e1c b\u01b0\u1edbc sau:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>B\u01b0\u1edbc 1: L\u1ea5y s\u1ed1 c\u1ea7n ki\u1ec3m tra<\/li>\n\n\n\n<li>B\u01b0\u1edbc 2: Chia s\u1ed1 \u0111\u00f3 cho 2 \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh s\u1ed1 d\u01b0<\/li>\n\n\n\n<li>B\u01b0\u1edbc 3: Ki\u1ec3m tra s\u1ed1 d\u01b0 \u0111\u00f3 c\u00f3 b\u1eb1ng 0 hay kh\u00f4ng, n\u1ebfu b\u1eb1ng 0 th\u00ec \u0111\u00f3 l\u00e0 s\u1ed1 ch\u1eb5n, ng\u01b0\u1ee3c l\u1ea1i \u0111\u00f3 l\u00e0 s\u1ed1 l\u1ebb.<\/li>\n<\/ul>\n\n\n\n<p>\u0110\u1ec3 gi\u1ea3i b\u00e0i to\u00e1n n\u00e0y qua ng\u00f4n ng\u1eef PHP th\u00ec tr\u01b0\u1edbc ti\u00ean ta t\u00ecm hi\u1ec3u&nbsp;<strong>c\u00fa ph\u00e1p c\u00e2u \u0111i\u1ec1u ki\u1ec7n if else trong php&nbsp;<\/strong>\u0111\u00e3 nh\u00e9.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. C\u00e2u l\u1ec7nh \u0111i\u1ec1u ki\u1ec7n if<\/h3>\n\n\n\n<p>C\u00e2u l\u1ec7nh if cho ph\u00e9p ta \u0111\u01b0a ra c\u00e1c quy\u1ebft \u0111\u1ecbnh d\u1ef1a tr\u00ean vi\u1ec7c ki\u1ec3m tra \u0111i\u1ec1u ki\u1ec7n n\u00e0o \u0111\u00f3 \u0111\u00fang (true) hay sai (false). C\u00fa ph\u00e1p nh\u01b0 sau:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nif ($bieuthuc) {\n    \/\/ C\u00e1c C\u00e2u L\u1ec7nh\n}\n<\/pre><\/div>\n\n\n<p><strong>V\u00ed d\u1ee5:&nbsp;<\/strong>Ch\u01b0\u01a1ng tr\u00ecnh ki\u1ec3m tra m\u1ed9t s\u1ed1 ch\u1eb5n hay l\u1ebb<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n$so_can_kiem_tra = 12;\n\nif ( ($so_can_kiem_tra % 2) == 0 ) {\n     echo &quot;S\u1ed1 {$so_can_kiem_tra} l\u00e0 s\u1ed1 ch\u1eb5n&quot;;\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. C\u00e2u l\u1ec7nh If else:<\/h3>\n\n\n\n<p><strong>L\u1ec7nh if<\/strong>&nbsp;d\u00f9ng \u0111\u1ec3 ki\u1ec3m tra m\u1ed9t \u0111i\u1ec1u ki\u1ec7n c\u00f3 \u0111\u00fang hay kh\u00f4ng? Gi\u1ea3 s\u1eed tr\u01b0\u1eddng h\u1ee3p ng\u01b0\u1ee3c l\u1ea1i \u0111i\u1ec1u ki\u1ec7n kh\u00f4ng \u0111\u00fang th\u00ec s\u1ebd th\u1ef1c hi\u1ec7n \u0111i\u1ec1u g\u00ec ? \u0111\u1ec3 gi\u1ea3i \u0111\u00e1p c\u00e2u h\u1ecfi n\u00e0y ta s\u1ebd nghi\u00ean c\u1ee9u \u0111\u1ebfn&nbsp;<strong>l\u1ec7nh if else trong php<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nif ($bieuthuc) {\n    \/\/ Nh\u1eefng C\u00e2u L\u1ec7nh 1;\n} else {\n    \/\/ Nh\u1eefng c\u00e2u l\u1ec7nh 2;\n}\n<\/pre><\/div>\n\n\n<p><strong>Gi\u1ea3i th\u00edch \u00fd ngh\u0129a<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>N\u1ebfu&nbsp;<code>$bieuthuc<\/code>&nbsp;\u0111\u00fang th\u00ec Nh\u1eefng C\u00e2u L\u1ec7nh 1 s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n v\u00e0 Nh\u1eefng C\u00e2u L\u1ec7nh 2 s\u1ebd kh\u00f4ng \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n.<\/li>\n\n\n\n<li>Ng\u01b0\u1ee3c l\u1ea1i th\u00ec n\u00f3 s\u1ebd kh\u00f4ng c\u1ea7n ki\u1ec3m tra n\u1eefa v\u00e0 th\u1ef1c hi\u1ec7n Nh\u1eefng C\u00e2u L\u1ec7nh 2.<\/li>\n<\/ul>\n\n\n\n<p><strong>V\u00ed d\u1ee5:&nbsp;<\/strong>ki\u1ec3m tra n\u0103m nay l\u00e0 n\u0103m ch\u1eb5n hay n\u0103m l\u1ebd, xu\u1ea5t ra m\u00e0n h\u00ecnh k\u1ebft qu\u1ea3 ch\u1eb5n hay l\u1ebd.<\/p>\n\n\n\n<p>B\u01b0\u1edbc 1: Nh\u1eadp n\u0103m<br>B\u01b0\u1edbc 2: Chia cho 2 v\u00e0 l\u1ea5y s\u1ed1 d\u01b0<br>B\u01b0\u1edbc 3<strong>:<\/strong>&nbsp;Ki\u1ec3m tra xem s\u1ed1 d\u01b0 c\u00f3 b\u1eb1ng 0 hay kh\u00f4ng? N\u1ebfu b\u1eb1ng kh\u00f4ng l\u00e0 n\u0103m ch\u1eb5n, ng\u01b0\u1ee3c l\u1ea1i l\u00e0 n\u0103m l\u1ebd<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n$nam = 2023;\n\nif ( ($nam % 2) == 0) {\n    echo &#039;N\u0103m &#039; . $nam . &#039; L\u00e0 N\u0103m Ch\u1eb5n&#039;;\n} else {\n    echo &#039;N\u0103m &#039; . $nam . &#039; L\u00e0 N\u0103m L\u1ebb&#039;;\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">4. C\u00e2u l\u1ec7nh if &#8211; if else &#8211; else trong php<\/h3>\n\n\n\n<p>Ch\u00fang ta c\u00f3 th\u1ec3 d\u00f9ng nhi\u1ec1u \u0111i\u1ec1u ki\u1ec7n kh\u00e1c nhau \u0111\u1ec3 r\u1ebb nh\u00e1nh:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nif ($bien == $dieu_kien_1 ){\n    echo &#039;N\u1ed9i dung 1&#039;;\n} else if ($bien == $dieu_kien_2) {\n    echo &#039;N\u1ed9i dung 2&#039;;\n} else if ($bien == $dieu_kien_3) {\n    echo &#039;N\u1ed9i dung 3&#039;;\n} else {\n    echo &#039;N\u1ed9i dung kh\u00e1c;\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5. C\u00e2u l\u1ec7nh if else l\u1ed3ng nhau<\/h3>\n\n\n\n<p>\u0110\u00e2y l\u00e0 ph\u1ea7n kh\u00f3 nh\u1ea5t trong t\u1ea5t c\u1ea3 c\u00e1c v\u1ea5n \u0111\u1ec1 c\u1ee7a c\u00e2u l\u1ec7nh if else trong php, \u1edf b\u00ean tr\u00ean ta ch\u1ec9 ch\u1ea1y c\u00e2u l\u1ec7nh if else m\u1ed9t t\u1ea7ng, \u1edf ph\u1ea7n n\u00e0y ta s\u1ebd nghi\u00ean c\u1ee9u \u0111\u1ebfn if else nhi\u1ec1u t\u1ea7ng l\u1ed3ng nhau, c\u00f3 ngh\u0129a l\u00e0 c\u00e2u if con n\u1eb1m trong c\u00e2u if cha.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nif ($bieu_thuc_cha) {\n    \/\/ C\u00e1c c\u00e2u l\u1ec7nh thu\u1ed9c v\u1ec1 bi\u1ec3u th\u1ee9c cha;\n    if ($bieu_thuc_con) {\n        \/\/ C\u00e1c c\u00e2u l\u1ec7nh thu\u1ed9c v\u1ec1 bi\u1ec3u th\u1ee9c con;\n    }\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>C\u00e1c v\u1ea5n \u0111\u1ec1 ta \u0111\u1ec1 c\u1eadp t\u1eeb \u0111\u1ea7u b\u00e0i cho \u0111\u1ebfn nay ch\u1ec9 cho ph\u00e9p ch\u00fang ta vi\u1ebft ch\u01b0\u01a1ng tr\u00ecnh [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":4572,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[178],"tags":[],"class_list":["post-8191","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"views":542,"_links":{"self":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/8191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/comments?post=8191"}],"version-history":[{"count":8,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/8191\/revisions"}],"predecessor-version":[{"id":8456,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/8191\/revisions\/8456"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media\/4572"}],"wp:attachment":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media?parent=8191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/categories?post=8191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/tags?post=8191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}