{"id":2973,"date":"2025-03-18T23:55:43","date_gmt":"2025-03-18T15:55:43","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2973"},"modified":"2025-03-18T23:55:43","modified_gmt":"2025-03-18T15:55:43","slug":"php-ifelse-%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/18\/php-ifelse-%e8%af%ad%e5%8f%a5\/","title":{"rendered":"PHP If\u2026Else \u8bed\u53e5"},"content":{"rendered":"\n<p><code>if...else<\/code> \u8bed\u53e5\u7528\u4e8e\u5728 PHP \u4e2d\u6267\u884c\u6761\u4ef6\u5224\u65ad\u3002\u6839\u636e\u7ed9\u5b9a\u6761\u4ef6\u7684\u771f\u5047\uff0c\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>\u57fa\u672c\u8bed\u6cd5<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition) {\n    \/\/ \u5982\u679c\u6761\u4ef6\u4e3a true \u6267\u884c\u7684\u4ee3\u7801\n} else {\n    \/\/ \u5982\u679c\u6761\u4ef6\u4e3a false \u6267\u884c\u7684\u4ee3\u7801\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u89e3\u91ca<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>if<\/code> \u8bed\u53e5\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u6761\u4ef6\u8868\u8fbe\u5f0f\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u6761\u4ef6\u4e3a <code>true<\/code>\uff0c\u5219\u6267\u884c <code>if<\/code> \u540e\u9762\u7684\u5927\u62ec\u53f7\u4e2d\u7684\u4ee3\u7801\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u6761\u4ef6\u4e3a <code>false<\/code>\uff0c\u5219\u6267\u884c <code>else<\/code> \u540e\u9762\u7684\u5927\u62ec\u53f7\u4e2d\u7684\u4ee3\u7801\uff08\u5982\u679c\u6709 <code>else<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$a = 10;\n$b = 5;\n\nif ($a &gt; $b) {\n    echo \"$a is greater than $b\"; \/\/ \u8f93\u51fa: 10 is greater than 5\n} else {\n    echo \"$a is less than or equal to $b\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>If\u2026Else \u8bed\u53e5\u7684\u6269\u5c55<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. If\u2026Elseif\u2026Else \u8bed\u53e5<\/strong><\/h3>\n\n\n\n<p>\u5f53\u6709\u591a\u4e2a\u6761\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>elseif<\/code> \u6765\u6dfb\u52a0\u66f4\u591a\u7684\u6761\u4ef6\u5224\u65ad\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition1) {\n    \/\/ \u5982\u679c\u6761\u4ef61\u4e3atrue\n} elseif (condition2) {\n    \/\/ \u5982\u679c\u6761\u4ef62\u4e3atrue\n} else {\n    \/\/ \u5982\u679c\u4e0a\u8ff0\u6761\u4ef6\u90fd\u4e3afalse\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$a = 10;\n$b = 5;\n$c = 8;\n\nif ($a &gt; $b) {\n    echo \"$a is greater than $b\"; \/\/ \u8f93\u51fa: 10 is greater than 5\n} elseif ($a &gt; $c) {\n    echo \"$a is greater than $c\"; \/\/ \u8f93\u51fa: 10 is greater than 8\n} else {\n    echo \"$a is less than or equal to both $b and $c\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u5d4c\u5957 If \u8bed\u53e5<\/strong><\/h3>\n\n\n\n<p>\u4f60\u4e5f\u53ef\u4ee5\u5728 <code>if<\/code> \u6216 <code>else<\/code> \u5757\u5185\u518d\u4f7f\u7528 <code>if<\/code> \u8bed\u53e5\uff0c\u8fd9\u79cd\u7ed3\u6784\u53eb\u505a\u5d4c\u5957 <code>if<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition1) {\n    if (condition2) {\n        \/\/ \u5982\u679c condition1 \u548c condition2 \u90fd\u4e3atrue\n    } else {\n        \/\/ \u5982\u679c condition1 \u4e3atrue\uff0c\u4f46 condition2 \u4e3afalse\n    }\n} else {\n    \/\/ \u5982\u679c condition1 \u4e3afalse\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$a = 10;\n$b = 5;\n$c = 3;\n\nif ($a &gt; $b) {\n    if ($a &gt; $c) {\n        echo \"$a is greater than both $b and $c\"; \/\/ \u8f93\u51fa: 10 is greater than both 5 and 3\n    } else {\n        echo \"$a is greater than $b but less than or equal to $c\";\n    }\n} else {\n    echo \"$a is less than or equal to $b\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u6761\u4ef6\u8868\u8fbe\u5f0f\u7b80\u5316\uff1a\u4e09\u5143\u8fd0\u7b97\u7b26<\/strong><\/h3>\n\n\n\n<p>PHP \u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5316\u7684\u6761\u4ef6\u8bed\u53e5\uff1a\u4e09\u5143\u8fd0\u7b97\u7b26\uff08Ternary Operator\uff09\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u66ff\u4ee3\u7b80\u5355\u7684 <code>if...else<\/code> \u8bed\u53e5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4e09\u5143\u8fd0\u7b97\u7b26\u8bed\u6cd5\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>(condition) ? true_expression : false_expression;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$a = 10;\n$b = 5;\n\necho ($a &gt; $b) ? \"$a is greater than $b\" : \"$a is less than or equal to $b\"; \n\/\/ \u8f93\u51fa: 10 is greater than 5\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3\uff1a<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>if...else<\/code> \u8bed\u53e5\u7528\u4e8e\u57fa\u4e8e\u6761\u4ef6\u6765\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u4f7f\u7528 <code>elseif<\/code> \u6765\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\u3002<\/li>\n\n\n\n<li>\u5d4c\u5957 <code>if<\/code> \u8bed\u53e5\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u903b\u8f91\u5224\u65ad\u3002<\/li>\n\n\n\n<li>\u4e09\u5143\u8fd0\u7b97\u7b26\u53ef\u4ee5\u7b80\u5316\u7b80\u5355\u7684\u6761\u4ef6\u5224\u65ad\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>if&#8230;else \u8bed\u53e5\u7528\u4e8e\u5728 PHP \u4e2d\u6267\u884c\u6761\u4ef6\u5224\u65ad\u3002\u6839\u636e\u7ed9\u5b9a\u6761\u4ef6\u7684\u771f\u5047\uff0c\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002 1. \u57fa\u672c\u8bed\u6cd5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2974,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-2973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2973"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2973\/revisions"}],"predecessor-version":[{"id":2975,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2973\/revisions\/2975"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2974"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}