{"id":421,"date":"2021-03-15T07:15:24","date_gmt":"2021-03-15T07:15:24","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=421"},"modified":"2025-04-05T14:54:32","modified_gmt":"2025-04-05T14:54:32","slug":"php-and-operator","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-and-operator\/","title":{"rendered":"PHP AND Operator"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the PHP AND operator and how to use it to build a complex logical expression.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-and-operator'>Introduction to the PHP AND operator <a href=\"#introduction-to-the-php-and-operator\" class=\"anchor\" id=\"introduction-to-the-php-and-operator\" title=\"Anchor for Introduction to the PHP AND operator\">#<\/a><\/h2>\n\n\n\n<p>The logical AND operator accepts two operands and returns <code>true<\/code> if both operands are <code>true<\/code>; otherwise, it returns <code>false<\/code>.<\/p>\n\n\n\n<p>PHP uses the <code>and<\/code> keyword to represent the logical AND operator:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">expression1 and expression2<\/code><\/span><\/pre>\n\n\n<p>The following table illustrates the result of the <code>and<\/code> operator:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>expression1<\/th><th>expression2<\/th><th>expression1 and expression2<\/th><\/tr><\/thead><tbody><tr><td><code>true<\/code><\/td><td><code>true<\/code><\/td><td><code>true<\/code><\/td><\/tr><tr><td><code>true<\/code><\/td><td><code>false<\/code><\/td><td><code>false<\/code><\/td><\/tr><tr><td><code>false<\/code><\/td><td><code>true<\/code><\/td><td><code>false<\/code><\/td><\/tr><tr><td><code>false<\/code><\/td><td><code>false<\/code><\/td><td><code>false<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Since PHP keywords are case-insensitive, the <code>AND<\/code> and <code>and<\/code> operators are the same:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">expression1 AND expression2<\/code><\/span><\/pre>\n\n\n<p>By convention, you should use the <code>and<\/code> operator in the lowercase format.<\/p>\n\n\n\n<p>In addition to using the <code>and<\/code> keyword, PHP uses <code>&amp;&amp;<\/code> as the logical AND operator:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">expression1 &amp;&amp; expression2<\/code><\/span><\/pre>\n\n\n<p>The <code>&amp;&amp;<\/code> and <code>and<\/code> operators return the same result. The only difference between the <code>&amp;&amp;<\/code> and <code>and<\/code> operators are their precedences.<\/p>\n\n\n\n<p>The <code>and<\/code> operator has higher precedence than the <code>&amp;&amp;<\/code> operator. The precedence of an operator specifies the order in which PHP evaluates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-and-operator-examples'>PHP AND operator examples <a href=\"#php-and-operator-examples\" class=\"anchor\" id=\"php-and-operator-examples\" title=\"Anchor for PHP AND operator examples\">#<\/a><\/h2>\n\n\n\n<p>Suppose you want to offer discounts to customers who buy more than three items at a price of more than 99. To determine whether customers can get a discount or not, you can use the AND operator as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$price = <span class=\"hljs-number\">100<\/span>;\n$qty = <span class=\"hljs-number\">5<\/span>;\n\n$discounted = $qty &gt; <span class=\"hljs-number\">3<\/span> &amp;&amp; $price &gt; <span class=\"hljs-number\">99<\/span>;\n\n\nvar_dump($discounted);\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRwcmljZSA9IDEwMDsKJHF0eSA9IDU7CgokZGlzY291bnRlZCA9ICRxdHkgPiAzICYmICRwcmljZSA-IDk5OwoKCnZhcl9kdW1wKCRkaXNjb3VudGVkKTs\" 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-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">bool(<span class=\"hljs-literal\">true<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you change the <code>$qty<\/code> to <code>2<\/code>, the <code>$discounted<\/code> will be <code>false<\/code> like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$price = <span class=\"hljs-number\">100<\/span>;\n$qty = <span class=\"hljs-number\">2<\/span>;\n\n$discounted = $qty &gt; <span class=\"hljs-number\">3<\/span> &amp;&amp; $price &gt; <span class=\"hljs-number\">99<\/span>;\n\nvar_dump($discounted);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRwcmljZSA9IDEwMDsKJHF0eSA9IDI7CgokZGlzY291bnRlZCA9ICRxdHkgPiAzICYmICRwcmljZSA-IDk5OwoKdmFyX2R1bXAoJGRpc2NvdW50ZWQpOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In practice, you&#8217;ll use the logical AND operator in the <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if\/\">if<\/a>, <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if-else\/\">if-else<\/a>, <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if-elseif\/\">if-elseif<\/a>, <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-while\/\">while<\/a>, and <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-do-while\/\">do-while <\/a>statements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='short-circuiting'>Short-circuiting <a href=\"#short-circuiting\" class=\"anchor\" id=\"short-circuiting\" title=\"Anchor for Short-circuiting\">#<\/a><\/h2>\n\n\n\n<p>When the value of the first operand is <code>false<\/code>, the logical AND operator knows that the result must also be <code>false<\/code>. In this case, it doesn&#8217;t evaluate the second operand. This process is called short-circuiting.<\/p>\n\n\n\n<p>See the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$debug = <span class=\"hljs-keyword\">false<\/span>;\n$debug &amp;&amp; <span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">'PHP and operator demo!'<\/span>);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRkZWJ1ZyA9IGZhbHNlOwokZGVidWcgJiYgcHJpbnQoJ1BIUCBhbmQgb3BlcmF0b3IgZGVtbyEnKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define the variable <code>$debug<\/code> and initialize it to <code>false<\/code>.<\/li>\n\n\n\n<li>Second, use the logical AND operator to combine the <code>$debug<\/code> and <code>print()<\/code>. Since <code>$debug<\/code> is <code>false<\/code>, PHP doesn&#8217;t evaluate the call to the <code>print()<\/code> function.<\/li>\n<\/ul>\n\n\n\n<p>If you change the <code>$debug<\/code> to <code>true<\/code>, you&#8217;ll see a message in the output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$debug = <span class=\"hljs-keyword\">true<\/span>;\n$debug &amp;&amp; <span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">'PHP and operator demo!'<\/span>);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRkZWJ1ZyA9IHRydWU7CiRkZWJ1ZyAmJiBwcmludCgnUEhQIGFuZCBvcGVyYXRvciBkZW1vIScpOw\" 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-6\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">PHP and operator demo!<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/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 PHP AND operator (<code>and<\/code>, <code>&amp;&amp;<\/code>) to combine two boolean expressions and return true if both expressions evaluate to true; otherwise, it returns <code>false<\/code>.<\/li>\n\n\n\n<li>The logical AND operator is short-circuiting.<\/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=\"421\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-and-operator\/\"\n\t\t\t\tdata-post-title=\"PHP AND Operator\"\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=\"421\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-and-operator\/\"\n\t\t\t\tdata-post-title=\"PHP AND Operator\"\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>In this tutorial, you will learn how to use the PHP AND operator to build complex logical expressions.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":20,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-421","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/421","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=421"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/421\/revisions"}],"predecessor-version":[{"id":2983,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/421\/revisions\/2983"}],"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=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}