{"id":382,"date":"2021-03-15T02:56:24","date_gmt":"2021-03-15T02:56:24","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=382"},"modified":"2025-04-06T01:45:54","modified_gmt":"2025-04-06T01:45:54","slug":"php-ternary-operator","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-ternary-operator\/","title":{"rendered":"PHP Ternary Operator"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn to use the PHP ternary operator to make the code shorter and more readable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-ternary-operator'>Introduction to the PHP ternary operator <a href=\"#introduction-to-the-php-ternary-operator\" class=\"anchor\" id=\"introduction-to-the-php-ternary-operator\" title=\"Anchor for Introduction to the PHP ternary operator\">#<\/a><\/h2>\n\n\n\n<p>The ternary operator is a shorthand for the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if-else\/\">if...else<\/a><\/code> statement. Instead of writing this:<\/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<span class=\"hljs-keyword\">if<\/span> (condition) {\n\t$result = value1;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t$result = value2;\n}\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>you can use this:<\/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\">$result = condition ? value1 : value2;<\/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>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First. PHP evaluates the <code>condition<\/code>. If it&#8217;s true, the right-hand expression returns the <code>value1<\/code>; otherwise, it returns the <code>value2<\/code>. <\/li>\n\n\n\n<li>Second, PHP assigns the result of the right-hand expression to the <code>$result<\/code> variable.<\/li>\n<\/ul>\n\n\n\n<p>As you can see, by using the ternary operator, you can make the code more concise.<\/p>\n\n\n\n<p class=\"note\">Note that the name ternary operator comes from the fact that this operator requires three operands: <code>expression<\/code>, <code>value1<\/code>, <code>value2<\/code>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-ternary-operator-example'>PHP ternary operator example <a href=\"#php-ternary-operator-example\" class=\"anchor\" id=\"php-ternary-operator-example\" title=\"Anchor for PHP ternary operator example\">#<\/a><\/h2>\n\n\n\n<p>Suppose you want to display the login link if the user has not logged in and the logout link if the user has already logged in. To do that, you can use the <code>if...else<\/code> statement as follows:<\/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$is_user_logged_in = <span class=\"hljs-keyword\">false<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> ($is_user_logged_in) {\n\t$title = <span class=\"hljs-string\">'Logout'<\/span>;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t$title = <span class=\"hljs-string\">'Login'<\/span>;\n}\n\n<span class=\"hljs-keyword\">echo<\/span> $title;<\/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=PD9waHAKCiRpc191c2VyX2xvZ2dlZF9pbiA9IGZhbHNlOwoKaWYgKCRpc191c2VyX2xvZ2dlZF9pbikgewoJJHRpdGxlID0gJ0xvZ291dCc7Cn0gZWxzZSB7CgkkdGl0bGUgPSAnTG9naW4nOwp9CgplY2hvICR0aXRsZTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, the <code>$title<\/code> will be <code>'Login'<\/code> because the <code>$is_user_logged_in<\/code> is set to <code>false<\/code>. The code is quite lengthy. And you can make it shorter by using the ternary operator as follows:<\/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$is_user_logged_in = <span class=\"hljs-keyword\">false<\/span>;\n\n$title = $is_user_logged_in ? <span class=\"hljs-string\">'Logout'<\/span> : <span class=\"hljs-string\">'Login'<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> $title;<\/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=PD9waHAKCiRpc191c2VyX2xvZ2dlZF9pbiA9IGZhbHNlOwoKJHRpdGxlID0gJGlzX3VzZXJfbG9nZ2VkX2luID8gJ0xvZ291dCcgOiAnTG9naW4nOwoKZWNobyAkdGl0bGU7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>It&#8217;s much shorter now. If the line is long, you can always break it down like this:<\/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$is_user_logged_in = <span class=\"hljs-keyword\">false<\/span>;\n\n$title = $is_user_logged_in\n\t\t\t? <span class=\"hljs-string\">'Logout'<\/span>\n\t\t\t: <span class=\"hljs-string\">'Login'<\/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=PD9waHAKCiRpc191c2VyX2xvZ2dlZF9pbiA9IGZhbHNlOwoKJHRpdGxlID0gJGlzX3VzZXJfbG9nZ2VkX2luCgkJCT8gJ0xvZ291dCcKCQkJOiAnTG9naW4nOwoKZWNobyAkdGl0bGU7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-shorthand-ternary-operator'>The shorthand ternary operator <a href=\"#the-shorthand-ternary-operator\" class=\"anchor\" id=\"the-shorthand-ternary-operator\" title=\"Anchor for The shorthand ternary operator\">#<\/a><\/h2>\n\n\n\n<p>Starting from PHP 5.3, you can use the shorthand ternary operator as follows:<\/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\">$result = $initial ?: $default;<\/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>In this syntax, PHP evaluates <code>$initial<\/code> in the boolean context. If <code>$initial<\/code> is true, PHP assigns the value of the <code>$initial<\/code> to the <code>$result<\/code> variable. Otherwise, it assigns the <code>$default<\/code> to the <code>$result<\/code> variable.<\/p>\n\n\n\n<p>The following example uses the shorthand ternary operator to assign the value of the <code>$path<\/code> to the <code>$url<\/code> if the <code>$path<\/code> is not empty. If the <code>$path<\/code> is empty, the ternary operator assigns the literal string &#8216;\/&#8217; to the <code>$url<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" 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$path = <span class=\"hljs-string\">'\/about'<\/span>;\n$url = $path ?: <span class=\"hljs-string\">'\/'<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> $url; <span class=\"hljs-comment\">\/\/ \/about<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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=PD9waHAKCiRwYXRoID0gJy9hYm91dCc7CiR1cmwgPSAkcGF0aCA_OiAnLyc7CgplY2hvICR1cmw7IC8vIC9hYm91dA\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">\/about<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='chaining-ternary-operators'>Chaining ternary operators <a href=\"#chaining-ternary-operators\" class=\"anchor\" id=\"chaining-ternary-operators\" title=\"Anchor for Chaining ternary operators\">#<\/a><\/h2>\n\n\n\n<p>Technically, you can chain ternary operators by using parentheses.<\/p>\n\n\n\n<p>Suppose you want to show various messages if users are eligible and have enough credit. The following example chains two ternary operators:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" 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$eligible = <span class=\"hljs-keyword\">true<\/span>;\n$has_credit = <span class=\"hljs-keyword\">false<\/span>;\n\n$message = $eligible\n\t\t\t? ($has_credit\n\t\t\t\t\t? <span class=\"hljs-string\">'Can use the credit'<\/span>\n\t\t\t\t\t: <span class=\"hljs-string\">'Not enough credit'<\/span>)\n\t\t\t: <span class=\"hljs-string\">'Not eligible to buy'<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> $message;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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=PD9waHAKCiRlbGlnaWJsZSA9IHRydWU7CiRoYXNfY3JlZGl0ID0gZmFsc2U7CgokbWVzc2FnZSA9ICRlbGlnaWJsZQoJCQk_ICgkaGFzX2NyZWRpdAoJCQkJCT8gJ0NhbiB1c2UgdGhlIGNyZWRpdCcKCQkJCQk6ICdOb3QgZW5vdWdoIGNyZWRpdCcpCgkJCTogJ05vdCBlbGlnaWJsZSB0byBidXknOwoKZWNobyAkbWVzc2FnZTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Most of the time, chaining multiple ternary operators makes the code more difficult to read. In this case, it&#8217;s better to use <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if-else\/\">if...else<\/a><\/code> or <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if-elseif\/\">if...elseif<\/a><\/code> statement.<\/p>\n\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>The ternary operator (<code>?:<\/code>) is a shorthand for the <code>if...else<\/code> statement.<\/li>\n\n\n\n<li>Do use the ternary operator when it makes your code more concise and more readable.<\/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=\"382\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-ternary-operator\/\"\n\t\t\t\tdata-post-title=\"PHP Ternary 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=\"382\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-ternary-operator\/\"\n\t\t\t\tdata-post-title=\"PHP Ternary 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 Ternary Operator to make the code shorter and more readable.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":26,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-382","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/382","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=382"}],"version-history":[{"count":4,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/382\/revisions"}],"predecessor-version":[{"id":2998,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/382\/revisions\/2998"}],"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=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}