{"id":224,"date":"2021-03-08T23:30:10","date_gmt":"2021-03-08T23:30:10","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=224"},"modified":"2025-04-06T01:41:25","modified_gmt":"2025-04-06T01:41:25","slug":"php-if-elseif","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-if-elseif\/","title":{"rendered":"PHP if elseif"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the PHP <code>if elseif<\/code> statement to execute code blocks based on multiple boolean expressions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-if-elseif-statement'>Introduction to the PHP if elseif statement <a href=\"#introduction-to-the-php-if-elseif-statement\" class=\"anchor\" id=\"introduction-to-the-php-if-elseif-statement\" title=\"Anchor for Introduction to the PHP if elseif statement\">#<\/a><\/h2>\n\n\n\n<p>The <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if\/\">if<\/a><\/code> statement evaluates one <code>expression<\/code> and executes a code block if the <code>expression<\/code> is true:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">if<\/span> (expression) {\n\tstatement;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>if<\/code> statement can have one or more optional <code>elseif<\/code> clauses. The <code>elseif<\/code> is a combination of <code>if<\/code> and <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if-else\/\">else<\/a><\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">if<\/span> (expression1) {\n\tstatement;\n} <span class=\"hljs-keyword\">elseif<\/span> (expression2) {\n\tstatement;\n} <span class=\"hljs-keyword\">elseif<\/span> (expression3) {\n\tstatement;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>PHP evaluates the <code>expression1<\/code> and execute the code block in the <code>if<\/code> clause if the <code>expression1<\/code> is <code>true<\/code>.<\/p>\n\n\n\n<p>If the <code>expression1<\/code> is <code>false<\/code>, the <code>PHP<\/code> evaluates the <code>expression2<\/code> in the next <code>elseif<\/code> clause. If the result is <code>true<\/code>, then PHP executes the statement in that <code>elseif<\/code> block. Otherwise, PHP evaluates the <code>expression3<\/code>. <\/p>\n\n\n\n<p>If the <code>expression3<\/code> is <code>true<\/code>, PHP executes the block that follows the <code>elseif<\/code> clause. Otherwise, PHP ignores it.<\/p>\n\n\n\n<p class=\"note\">Notice that when an <code>if<\/code> statement has multiple <code>elseif<\/code> clauses, the <code>elseif<\/code> will execute only if the expression in the preceding <code>if<\/code> or <code>elseif<\/code> clause evaluates to <code>false<\/code>.<\/p>\n\n\n\n<p>The following flowchart illustrates how the <code>if elseif<\/code> statement works:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"466\" height=\"373\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/03\/php-if-elseif.png\" alt=\"\" class=\"wp-image-398\" srcset=\"https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/03\/php-if-elseif.png 466w, https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/03\/php-if-elseif-300x240.png 300w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><\/figure>\n<\/div>\n\n\n<p>The following example uses the <code>if elseif<\/code> statement to display whether the variable <code>$x<\/code> is greater than <code>$y<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$x = <span class=\"hljs-number\">10<\/span>;\n$y = <span class=\"hljs-number\">20<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> ($x &gt; $y) {\n\t$message = <span class=\"hljs-string\">'x is greater than y'<\/span>;\n} <span class=\"hljs-keyword\">elseif<\/span> ($x &lt; $y) {\n\t$message = <span class=\"hljs-string\">'x is less than y'<\/span>;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t$message = <span class=\"hljs-string\">'x is equal to y'<\/span>;\n}\n\n<span class=\"hljs-keyword\">echo<\/span> $message;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiR4ID0gMTA7CiR5ID0gMjA7CgppZiAoJHggPiAkeSkgewoJJG1lc3NhZ2UgPSAneCBpcyBncmVhdGVyIHRoYW4geSc7Cn0gZWxzZWlmICgkeCA8ICR5KSB7CgkkbWVzc2FnZSA9ICd4IGlzIGxlc3MgdGhhbiB5JzsKfSBlbHNlIHsKCSRtZXNzYWdlID0gJ3ggaXMgZXF1YWwgdG8geSc7Cn0KCmVjaG8gJG1lc3NhZ2U7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">x is less than y<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The script shows the message <code>x is less than y<\/code> as expected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-if-elseif-alternative-syntax'>PHP if elseif alternative syntax <a href=\"#php-if-elseif-alternative-syntax\" class=\"anchor\" id=\"php-if-elseif-alternative-syntax\" title=\"Anchor for PHP if elseif alternative syntax\">#<\/a><\/h2>\n\n\n\n<p>PHP also supports an alternative syntax for the <code>elseif<\/code> without using curly braces like the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">if<\/span> (expression):\n\tstatement;\n<span class=\"hljs-keyword\">elseif<\/span> (expression2):\n\tstatement;\n<span class=\"hljs-keyword\">elseif<\/span> (expression3):\n\tstatement;\n<span class=\"hljs-keyword\">endif<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use a semicolon (:)  after each condition following the <code>if<\/code> or <code>elseif<\/code> keyword.<\/li>\n\n\n\n<li>Use the <code>endif<\/code> keyword instead of a curly brace (<code>}<\/code>) at the end of the <code>if<\/code> statement.<\/li>\n<\/ul>\n\n\n\n<p>The following example uses the <code>elseif<\/code> alternative syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$x = <span class=\"hljs-number\">10<\/span>;\n$y = <span class=\"hljs-number\">20<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> ($x &gt; $y) :\n\t$message = <span class=\"hljs-string\">'x is greater than y'<\/span>;\n<span class=\"hljs-keyword\">elseif<\/span> ($x &lt; $y):\n\t$message = <span class=\"hljs-string\">'x is less than y'<\/span>;\n<span class=\"hljs-keyword\">else<\/span>:\n\t$message = <span class=\"hljs-string\">'x is equal to y'<\/span>;\n<span class=\"hljs-keyword\">endif<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> $message;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiR4ID0gMTA7CiR5ID0gMjA7CgppZiAoJHggPiAkeSkgOgoJJG1lc3NhZ2UgPSAneCBpcyBncmVhdGVyIHRoYW4geSc7CmVsc2VpZiAoJHggPCAkeSk6CgkkbWVzc2FnZSA9ICd4IGlzIGxlc3MgdGhhbiB5JzsKZWxzZToKCSRtZXNzYWdlID0gJ3ggaXMgZXF1YWwgdG8geSc7CmVuZGlmOwoKZWNobyAkbWVzc2FnZTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The alternative syntax is suitable for use with HTML.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-elseif-vs-else-if'>PHP elseif vs. else if <a href=\"#php-elseif-vs-else-if\" class=\"anchor\" id=\"php-elseif-vs-else-if\" title=\"Anchor for PHP elseif vs. else if\">#<\/a><\/h2>\n\n\n\n<p>PHP allows you to write <code>else if<\/code> (in two words) that has the same result as <code>elseif<\/code> (in a single word):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&lt;<span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">if<\/span> (expression) {\n\tstatement;\n} <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (expression2) {\n\tstatement2;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>else if<\/code> in this case, is the same as the following nested <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if-else\/\">if...else<\/a><\/code> structure:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span> (expression) {\n\tstatement;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t<span class=\"hljs-keyword\">if<\/span> (expression2) {\n\t\tstatement2;\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you use the alternative syntax, you need to use the <code>if...elseif<\/code> statement instead of the <code>if...else if<\/code> statement. Otherwise, you&#8217;ll get an error.<\/p>\n\n\n\n<p>The following example doesn&#8217;t work and cause an error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$x = <span class=\"hljs-number\">10<\/span>;\n$y = <span class=\"hljs-number\">20<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> ($x &gt; $y) :\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'x is greater than y'<\/span>;\n<span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> ($x &lt; $y):\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'x is equal to y'<\/span>;\n<span class=\"hljs-keyword\">else<\/span>:\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'x is less than y'<\/span>;\n<span class=\"hljs-keyword\">endif<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>if...elseif<\/code> statement to evaluate multiple expressions and execute code blocks conditionally.<\/li>\n\n\n\n<li>Only the <code>if...elseif<\/code> supports alternative syntax, the <code>if...else if<\/code> doesn&#8217;t.<\/li>\n\n\n\n<li>Do use the <code>elseif<\/code> whenever possible to make your code more consistent.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"224\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-if-elseif\/\"\n\t\t\t\tdata-post-title=\"PHP if elseif\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"224\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-if-elseif\/\"\n\t\t\t\tdata-post-title=\"PHP if elseif\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn about the PHP if elseif statement to execute code blocks based on multiple boolean expressions. Introduction to the PHP if elseif statement # The if statement evaluates one expression and executes a code block if the expression is true: The if statement can have one or more optional elseif [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":25,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-224","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=224"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/224\/revisions"}],"predecessor-version":[{"id":2994,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/224\/revisions\/2994"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}