CINXE.COM
PHP: defined - Manual
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP: defined - Manual </title> <link rel="icon" type="image/svg+xml" sizes="any" href="https://www.php.net/favicon.svg?v=2"> <link rel="icon" type="image/png" sizes="196x196" href="https://www.php.net/favicon-196x196.png?v=2"> <link rel="icon" type="image/png" sizes="32x32" href="https://www.php.net/favicon-32x32.png?v=2"> <link rel="icon" type="image/png" sizes="16x16" href="https://www.php.net/favicon-16x16.png?v=2"> <link rel="shortcut icon" href="https://www.php.net/favicon.ico?v=2"> <link rel="search" type="application/opensearchdescription+xml" href="http://php.net/phpnetimprovedsearch.src" title="Add PHP.net search"> <link rel="alternate" type="application/atom+xml" href="https://www.php.net/releases/feed.php" title="PHP Release feed"> <link rel="alternate" type="application/atom+xml" href="https://www.php.net/feed.atom" title="PHP: Hypertext Preprocessor"> <link rel="canonical" href="https://www.php.net/manual/en/function.defined.php"> <link rel="shorturl" href="https://www.php.net/defined"> <link rel="alternate" href="https://www.php.net/defined" hreflang="x-default"> <link rel="contents" href="https://www.php.net/manual/en/index.php"> <link rel="index" href="https://www.php.net/manual/en/ref.misc.php"> <link rel="prev" href="https://www.php.net/manual/en/function.define.php"> <link rel="next" href="https://www.php.net/manual/en/function.die.php"> <link rel="alternate" href="https://www.php.net/manual/en/function.defined.php" hreflang="en"> <link rel="alternate" href="https://www.php.net/manual/de/function.defined.php" hreflang="de"> <link rel="alternate" href="https://www.php.net/manual/es/function.defined.php" hreflang="es"> <link rel="alternate" href="https://www.php.net/manual/fr/function.defined.php" hreflang="fr"> <link rel="alternate" href="https://www.php.net/manual/it/function.defined.php" hreflang="it"> <link rel="alternate" href="https://www.php.net/manual/ja/function.defined.php" hreflang="ja"> <link rel="alternate" href="https://www.php.net/manual/pt_BR/function.defined.php" hreflang="pt_BR"> <link rel="alternate" href="https://www.php.net/manual/ru/function.defined.php" hreflang="ru"> <link rel="alternate" href="https://www.php.net/manual/tr/function.defined.php" hreflang="tr"> <link rel="alternate" href="https://www.php.net/manual/uk/function.defined.php" hreflang="uk"> <link rel="alternate" href="https://www.php.net/manual/zh/function.defined.php" hreflang="zh"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1707321815&f=/fonts/Fira/fira.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1707321815&f=/fonts/Font-Awesome/css/fontello.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1738926002&f=/styles/theme-base.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1730558402&f=/styles/theme-medium.css" media="screen"> <base href="https://www.php.net/manual/en/function.defined.php"> <meta name="Description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:site" content="@official_php" /> <meta name="twitter:title" content="PHP: Hypertext Preprocessor" /> <meta name="twitter:description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <meta name="twitter:creator" content="@official_php" /> <meta name="twitter:image:src" content="https://www.php.net/images/meta-image.png" /> <meta itemprop="name" content="PHP: Hypertext Preprocessor" /> <meta itemprop="description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <meta itemprop="image" content="https://www.php.net/images/meta-image.png" /> <meta property="og:image" content="https://www.php.net/images/meta-image.png" /> <meta property="og:description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <link href="https://fosstodon.org/@php" rel="me" /> <!-- Matomo --> <script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(["setDoNotTrack", true]); _paq.push(["disableCookies"]); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://analytics.php.net/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script> <!-- End Matomo Code --> </head> <body class="docs "> <nav class="navbar navbar-fixed-top"> <div class="navbar__inner"> <a href="/" aria-label="PHP Home" class="navbar__brand"> <img src="/images/logos/php-logo-white.svg" aria-hidden="true" width="80" height="40" > </a> <div id="navbar__offcanvas" tabindex="-1" class="navbar__offcanvas" aria-label="Menu" > <button id="navbar__close-button" class="navbar__icon-item navbar_icon-item--visually-aligned navbar__close-button" > <svg xmlns="http://www.w3.org/2000/svg" width="24" viewBox="0 0 24 24" fill="currentColor"><path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" /></svg> </button> <ul class="navbar__nav"> <li class="navbar__item"> <a href="/downloads.php" class="navbar__link " > Downloads </a> </li> <li class="navbar__item"> <a href="/docs.php" aria-current="page" class="navbar__link navbar__link--active " > Documentation </a> </li> <li class="navbar__item"> <a href="/get-involved.php" class="navbar__link " > Get Involved </a> </li> <li class="navbar__item"> <a href="/support.php" class="navbar__link " > Help </a> </li> <li class="navbar__item"> <a href="/releases/8.4/index.php" class="navbar__link navbar__release" > <img src="/images/php8/logo_php8_4.svg" alt="PHP 8.4"> </a> </li> </ul> </div> <div class="navbar__right"> <!-- Desktop default search --> <form action="/manual-lookup.php" class="navbar__search-form" > <label for="navbar__search-input" aria-label="Search docs"> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </label> <input type="search" name="pattern" id="navbar__search-input" class="navbar__search-input" placeholder="Search docs" accesskey="s" > <input type="hidden" name="scope" value="quickref"> </form> <!-- Desktop encanced search --> <button id="navbar__search-button" class="navbar__search-button" hidden > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> Search docs </button> <!-- Mobile default items --> <a id="navbar__search-link" href="/lookup-form.php" aria-label="Search docs" class="navbar__icon-item navbar__search-link" > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </a> <a id="navbar__menu-link" href="/menu.php" aria-label="Menu" class="navbar__icon-item navbar_icon-item--visually-aligned navbar_menu-link" > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="currentColor" > <path d="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z" /> </svg> </a> <!-- Mobile enhanced items --> <button id="navbar__search-button-mobile" aria-label="Search docs" class="navbar__icon-item navbar__search-button-mobile" hidden > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </button> <button id="navbar__menu-button" aria-label="Menu" class="navbar__icon-item navbar_icon-item--visually-aligned" hidden > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="currentColor" > <path d="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z" /> </svg> </button> </div> <div id="navbar__backdrop" class="navbar__backdrop" ></div> </div> <div id="flash-message"></div> </nav> <nav id="trick"><div><dl> <dt><a href='/manual/en/getting-started.php'>Getting Started</a></dt> <dd><a href='/manual/en/introduction.php'>Introduction</a></dd> <dd><a href='/manual/en/tutorial.php'>A simple tutorial</a></dd> <dt><a href='/manual/en/langref.php'>Language Reference</a></dt> <dd><a href='/manual/en/language.basic-syntax.php'>Basic syntax</a></dd> <dd><a href='/manual/en/language.types.php'>Types</a></dd> <dd><a href='/manual/en/language.variables.php'>Variables</a></dd> <dd><a href='/manual/en/language.constants.php'>Constants</a></dd> <dd><a href='/manual/en/language.expressions.php'>Expressions</a></dd> <dd><a href='/manual/en/language.operators.php'>Operators</a></dd> <dd><a href='/manual/en/language.control-structures.php'>Control Structures</a></dd> <dd><a href='/manual/en/language.functions.php'>Functions</a></dd> <dd><a href='/manual/en/language.oop5.php'>Classes and Objects</a></dd> <dd><a href='/manual/en/language.namespaces.php'>Namespaces</a></dd> <dd><a href='/manual/en/language.enumerations.php'>Enumerations</a></dd> <dd><a href='/manual/en/language.errors.php'>Errors</a></dd> <dd><a href='/manual/en/language.exceptions.php'>Exceptions</a></dd> <dd><a href='/manual/en/language.fibers.php'>Fibers</a></dd> <dd><a href='/manual/en/language.generators.php'>Generators</a></dd> <dd><a href='/manual/en/language.attributes.php'>Attributes</a></dd> <dd><a href='/manual/en/language.references.php'>References Explained</a></dd> <dd><a href='/manual/en/reserved.variables.php'>Predefined Variables</a></dd> <dd><a href='/manual/en/reserved.exceptions.php'>Predefined Exceptions</a></dd> <dd><a href='/manual/en/reserved.interfaces.php'>Predefined Interfaces and Classes</a></dd> <dd><a href='/manual/en/reserved.attributes.php'>Predefined Attributes</a></dd> <dd><a href='/manual/en/context.php'>Context options and parameters</a></dd> <dd><a href='/manual/en/wrappers.php'>Supported Protocols and Wrappers</a></dd> </dl> <dl> <dt><a href='/manual/en/security.php'>Security</a></dt> <dd><a href='/manual/en/security.intro.php'>Introduction</a></dd> <dd><a href='/manual/en/security.general.php'>General considerations</a></dd> <dd><a href='/manual/en/security.cgi-bin.php'>Installed as CGI binary</a></dd> <dd><a href='/manual/en/security.apache.php'>Installed as an Apache module</a></dd> <dd><a href='/manual/en/security.sessions.php'>Session Security</a></dd> <dd><a href='/manual/en/security.filesystem.php'>Filesystem Security</a></dd> <dd><a href='/manual/en/security.database.php'>Database Security</a></dd> <dd><a href='/manual/en/security.errors.php'>Error Reporting</a></dd> <dd><a href='/manual/en/security.variables.php'>User Submitted Data</a></dd> <dd><a href='/manual/en/security.hiding.php'>Hiding PHP</a></dd> <dd><a href='/manual/en/security.current.php'>Keeping Current</a></dd> <dt><a href='/manual/en/features.php'>Features</a></dt> <dd><a href='/manual/en/features.http-auth.php'>HTTP authentication with PHP</a></dd> <dd><a href='/manual/en/features.cookies.php'>Cookies</a></dd> <dd><a href='/manual/en/features.sessions.php'>Sessions</a></dd> <dd><a href='/manual/en/features.file-upload.php'>Handling file uploads</a></dd> <dd><a href='/manual/en/features.remote-files.php'>Using remote files</a></dd> <dd><a href='/manual/en/features.connection-handling.php'>Connection handling</a></dd> <dd><a href='/manual/en/features.persistent-connections.php'>Persistent Database Connections</a></dd> <dd><a href='/manual/en/features.commandline.php'>Command line usage</a></dd> <dd><a href='/manual/en/features.gc.php'>Garbage Collection</a></dd> <dd><a href='/manual/en/features.dtrace.php'>DTrace Dynamic Tracing</a></dd> </dl> <dl> <dt><a href='/manual/en/funcref.php'>Function Reference</a></dt> <dd><a href='/manual/en/refs.basic.php.php'>Affecting PHP's Behaviour</a></dd> <dd><a href='/manual/en/refs.utilspec.audio.php'>Audio Formats Manipulation</a></dd> <dd><a href='/manual/en/refs.remote.auth.php'>Authentication Services</a></dd> <dd><a href='/manual/en/refs.utilspec.cmdline.php'>Command Line Specific Extensions</a></dd> <dd><a href='/manual/en/refs.compression.php'>Compression and Archive Extensions</a></dd> <dd><a href='/manual/en/refs.crypto.php'>Cryptography Extensions</a></dd> <dd><a href='/manual/en/refs.database.php'>Database Extensions</a></dd> <dd><a href='/manual/en/refs.calendar.php'>Date and Time Related Extensions</a></dd> <dd><a href='/manual/en/refs.fileprocess.file.php'>File System Related Extensions</a></dd> <dd><a href='/manual/en/refs.international.php'>Human Language and Character Encoding Support</a></dd> <dd><a href='/manual/en/refs.utilspec.image.php'>Image Processing and Generation</a></dd> <dd><a href='/manual/en/refs.remote.mail.php'>Mail Related Extensions</a></dd> <dd><a href='/manual/en/refs.math.php'>Mathematical Extensions</a></dd> <dd><a href='/manual/en/refs.utilspec.nontext.php'>Non-Text MIME Output</a></dd> <dd><a href='/manual/en/refs.fileprocess.process.php'>Process Control Extensions</a></dd> <dd><a href='/manual/en/refs.basic.other.php'>Other Basic Extensions</a></dd> <dd><a href='/manual/en/refs.remote.other.php'>Other Services</a></dd> <dd><a href='/manual/en/refs.search.php'>Search Engine Extensions</a></dd> <dd><a href='/manual/en/refs.utilspec.server.php'>Server Specific Extensions</a></dd> <dd><a href='/manual/en/refs.basic.session.php'>Session Extensions</a></dd> <dd><a href='/manual/en/refs.basic.text.php'>Text Processing</a></dd> <dd><a href='/manual/en/refs.basic.vartype.php'>Variable and Type Related Extensions</a></dd> <dd><a href='/manual/en/refs.webservice.php'>Web Services</a></dd> <dd><a href='/manual/en/refs.utilspec.windows.php'>Windows Only Extensions</a></dd> <dd><a href='/manual/en/refs.xml.php'>XML Manipulation</a></dd> <dd><a href='/manual/en/refs.ui.php'>GUI Extensions</a></dd> </dl> <dl> <dt>Keyboard Shortcuts</dt><dt>?</dt> <dd>This help</dd> <dt>j</dt> <dd>Next menu item</dd> <dt>k</dt> <dd>Previous menu item</dd> <dt>g p</dt> <dd>Previous man page</dd> <dt>g n</dt> <dd>Next man page</dd> <dt>G</dt> <dd>Scroll to bottom</dd> <dt>g g</dt> <dd>Scroll to top</dd> <dt>g h</dt> <dd>Goto homepage</dd> <dt>g s</dt> <dd>Goto search<br>(current page)</dd> <dt>/</dt> <dd>Focus search box</dd> </dl></div></nav> <div id="goto"> <div class="search"> <div class="text"></div> <div class="results"><ul></ul></div> </div> </div> <div id="breadcrumbs" class="clearfix"> <div id="breadcrumbs-inner"> <div class="next"> <a href="function.die.php"> die » </a> </div> <div class="prev"> <a href="function.define.php"> « define </a> </div> <ul> <li><a href='index.php'>PHP Manual</a></li> <li><a href='funcref.php'>Function Reference</a></li> <li><a href='refs.basic.other.php'>Other Basic Extensions</a></li> <li><a href='book.misc.php'>Misc.</a></li> <li><a href='ref.misc.php'>Misc. Functions</a></li> </ul> </div> </div> <div id="layout" class="clearfix"> <section id="layout-content"> <div class="page-tools"> <div class="change-language"> <form action="/manual/change.php" method="get" id="changelang" name="changelang"> <fieldset> <label for="changelang-langs">Change language:</label> <select onchange="document.changelang.submit()" name="page" id="changelang-langs"> <option value='en/function.defined.php' selected="selected">English</option> <option value='de/function.defined.php'>German</option> <option value='es/function.defined.php'>Spanish</option> <option value='fr/function.defined.php'>French</option> <option value='it/function.defined.php'>Italian</option> <option value='ja/function.defined.php'>Japanese</option> <option value='pt_BR/function.defined.php'>Brazilian Portuguese</option> <option value='ru/function.defined.php'>Russian</option> <option value='tr/function.defined.php'>Turkish</option> <option value='uk/function.defined.php'>Ukrainian</option> <option value='zh/function.defined.php'>Chinese (Simplified)</option> <option value='help-translate.php'>Other</option> </select> </fieldset> </form> </div> </div><div id="function.defined" class="refentry"> <div class="refnamediv"> <h1 class="refname">defined</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">defined</span> — <span class="dc-title">Checks whether a constant with the given name exists</span></p> </div> <div class="refsect1 description" id="refsect1-function.defined-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="methodname"><strong>defined</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$constant_name</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div> <p class="para rdfs-comment"> Checks whether a constant with the given <code class="parameter">constant_name</code> is defined. </p> <p class="para"> This function works also with <a href="language.oop5.constants.php" class="link">class constants</a> and <a href="language.types.enumerations.php" class="link">enum cases</a>. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> If you want to see if a variable exists, use <span class="function"><a href="function.isset.php" class="function">isset()</a></span> as <span class="function"><strong>defined()</strong></span> only applies to <a href="language.constants.php" class="link">constants</a>. If you want to see if a function exists, use <span class="function"><a href="function.function-exists.php" class="function">function_exists()</a></span>. </p> </p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-function.defined-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt><code class="parameter">constant_name</code></dt> <dd> <p class="para"> The constant name. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.defined-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> if the named constant given by <code class="parameter">constant_name</code> has been defined, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> otherwise. </p> </div> <div class="refsect1 examples" id="refsect1-function.defined-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3656"> <p><strong>Example #1 Checking Constants</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">/* Note the use of quotes, this is important. This example is checking<br /> * if the string 'TEST' is the name of a constant named TEST */<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">defined</span><span style="color: #007700">(</span><span style="color: #DD0000">'TEST'</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #0000BB">TEST</span><span style="color: #007700">;<br />}<br /><br /><br />interface </span><span style="color: #0000BB">bar </span><span style="color: #007700">{<br /> const </span><span style="color: #0000BB">test </span><span style="color: #007700">= </span><span style="color: #DD0000">'foobar!'</span><span style="color: #007700">;<br />}<br /><br />class </span><span style="color: #0000BB">foo </span><span style="color: #007700">{<br /> const </span><span style="color: #0000BB">test </span><span style="color: #007700">= </span><span style="color: #DD0000">'foobar!'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">defined</span><span style="color: #007700">(</span><span style="color: #DD0000">'bar::test'</span><span style="color: #007700">)); </span><span style="color: #FF8000">// bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">defined</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo::test'</span><span style="color: #007700">)); </span><span style="color: #FF8000">// bool(true)<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <div class="example" id="example-3657"> <p><strong>Example #2 Checking Enum Cases (as of PHP 8.1.0)</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">enum </span><span style="color: #0000BB">Suit<br /></span><span style="color: #007700">{<br /> case </span><span style="color: #0000BB">Hearts</span><span style="color: #007700">;<br /> case </span><span style="color: #0000BB">Diamonds</span><span style="color: #007700">;<br /> case </span><span style="color: #0000BB">Clubs</span><span style="color: #007700">;<br /> case </span><span style="color: #0000BB">Spades</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">defined</span><span style="color: #007700">(</span><span style="color: #DD0000">'Suit::Hearts'</span><span style="color: #007700">)); </span><span style="color: #FF8000">// bool(true)<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.defined-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li><span class="function"><a href="function.define.php" class="function" rel="rdfs-seeAlso">define()</a> - Defines a named constant</span></li> <li><span class="function"><a href="function.constant.php" class="function" rel="rdfs-seeAlso">constant()</a> - Returns the value of a constant</span></li> <li><span class="function"><a href="function.get-defined-constants.php" class="function" rel="rdfs-seeAlso">get_defined_constants()</a> - Returns an associative array with the names of all the constants and their values</span></li> <li><span class="function"><a href="function.function-exists.php" class="function" rel="rdfs-seeAlso">function_exists()</a> - Return true if the given function has been defined</span></li> <li>The section on <a href="language.constants.php" class="link">Constants</a></li> </ul> </p> </div> </div> <div class="contribute"> <h3 class="title">Found A Problem?</h3> <div> </div> <div class="edit-bug"> <a href="https://github.com/php/doc-base/blob/master/README.md" title="This will take you to our contribution guidelines on GitHub" target="_blank" rel="noopener noreferrer">Learn How To Improve This Page</a> • <a href="https://github.com/php/doc-en/blob/master/reference/misc/functions/defined.xml">Submit a Pull Request</a> • <a href="https://github.com/php/doc-en/issues/new?body=From%20manual%20page:%20https:%2F%2Fphp.net%2Ffunction.defined%0A%0A---">Report a Bug</a> </div> </div><section id="usernotes"> <div class="head"> <span class="action"><a href="/manual/add-note.php?sect=function.defined&repo=en&redirect=https://www.php.net/manual/en/function.defined.php">+<small>add a note</small></a></span> <h3 class="title">User Contributed Notes <span class="count">15 notes</span></h3> </div><div id="allnotes"> <div class="note" id="84439"> <div class="votes"> <div id="Vu84439"> <a href="/manual/vote-note.php?id=84439&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd84439"> <a href="/manual/vote-note.php?id=84439&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V84439" title="76% like this..."> 126 </div> </div> <a href="#84439" class="name"> <strong class="user"><em>daniel at neville dot tk</em></strong></a><a class="genanchor" href="#84439"> ¶</a><div class="date" title="2008-07-14 10:48"><strong>16 years ago</strong></div> <div class="text" id="Hcom84439"> <div class="phpcode"><code><span class="html">My preferred way of checking if a constant is set, and if it isn't - setting it (could be used to set defaults in a file, where the user has already had the opportunity to set their own values in another.)<br /><br /><span class="default"><?php<br /><br />defined</span><span class="keyword">(</span><span class="string">'CONSTANT'</span><span class="keyword">) or </span><span class="default">define</span><span class="keyword">(</span><span class="string">'CONSTANT'</span><span class="keyword">, </span><span class="string">'SomeDefaultValue'</span><span class="keyword">);<br /><br /></span><span class="default">?><br /></span><br />Dan.</span></code></div> </div> </div> <div class="note" id="120920"> <div class="votes"> <div id="Vu120920"> <a href="/manual/vote-note.php?id=120920&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd120920"> <a href="/manual/vote-note.php?id=120920&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V120920" title="75% like this..."> 14 </div> </div> <a href="#120920" class="name"> <strong class="user"><em>ASchmidt at Anamera dot net</em></strong></a><a class="genanchor" href="#120920"> ¶</a><div class="date" title="2017-03-31 08:08"><strong>8 years ago</strong></div> <div class="text" id="Hcom120920"> <div class="phpcode"><code><span class="html">// Checking the existence of a class constant, if the class is referenced by a variable.<br /><br />class Class_A<br />{<br /> const CONST_A = 'value A';<br />}<br /><br />// When class name is known.<br />if ( defined( 'Class_A::CONST_A' ) )<br /> echo 'Class_A::CONST_A defined';<br /><br />// Using a class name variable. Note the double quotes.<br />$class_name = Class_A::class;<br />if ( defined( "$class_name::CONST_A" ) )<br /> echo '$class_name::CONST_A defined';<br /><br />// Using an instantiated object for a variable class.<br />$object_A = new $class_name();<br />if ( defined( get_class($object_A).'::CONST_A' ) )<br /> echo '$object_A::CONST_A defined';</span></code></div> </div> </div> <div class="note" id="106287"> <div class="votes"> <div id="Vu106287"> <a href="/manual/vote-note.php?id=106287&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd106287"> <a href="/manual/vote-note.php?id=106287&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V106287" title="70% like this..."> 21 </div> </div> <a href="#106287" class="name"> <strong class="user"><em>Lars Lernestal</em></strong></a><a class="genanchor" href="#106287"> ¶</a><div class="date" title="2011-10-25 02:03"><strong>13 years ago</strong></div> <div class="text" id="Hcom106287"> <div class="phpcode"><code><span class="html">if you want to check id a class constant is defined use self:: before the constant name:<br /><br /><span class="default"><?php<br />defined</span><span class="keyword">(</span><span class="string">'self::CONSTANT_NAME'</span><span class="keyword">);<br /></span><span class="default">?></span></span></code></div> </div> </div> <div class="note" id="89886"> <div class="votes"> <div id="Vu89886"> <a href="/manual/vote-note.php?id=89886&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd89886"> <a href="/manual/vote-note.php?id=89886&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V89886" title="68% like this..."> 25 </div> </div> <a href="#89886" class="name"> <strong class="user"><em>tris+php at tfconsulting dot com dot au</em></strong></a><a class="genanchor" href="#89886"> ¶</a><div class="date" title="2009-03-26 06:40"><strong>16 years ago</strong></div> <div class="text" id="Hcom89886"> <div class="phpcode"><code><span class="html">Before using defined() have a look at the following benchmarks:<br /><br />true 0.65ms<br />$true 0.69ms (1)<br />$config['true'] 0.87ms<br />TRUE_CONST 1.28ms (2)<br />true 0.65ms<br />defined('TRUE_CONST') 2.06ms (3)<br />defined('UNDEF_CONST') 12.34ms (4)<br />isset($config['def_key']) 0.91ms (5)<br />isset($config['undef_key']) 0.79ms<br />isset($empty_hash[$good_key]) 0.78ms<br />isset($small_hash[$good_key]) 0.86ms<br />isset($big_hash[$good_key]) 0.89ms<br />isset($small_hash[$bad_key]) 0.78ms<br />isset($big_hash[$bad_key]) 0.80ms<br /><br />PHP Version 5.2.6, Apache 2.0, Windows XP<br /><br />Each statement was executed 1000 times and while a 12ms overhead on 1000 calls isn't going to have the end users tearing their hair out, it does throw up some interesting results when comparing to if(true):<br /><br />1) if($true) was virtually identical<br />2) if(TRUE_CONST) was almost twice as slow - I guess that the substitution isn't done at compile time (I had to double check this one!)<br />3) defined() is 3 times slower if the constant exists<br />4) defined() is 19 TIMES SLOWER if the constant doesn't exist!<br />5) isset() is remarkably efficient regardless of what you throw at it (great news for anyone implementing array driven event systems - me!)<br /><br />May want to avoid if(defined('DEBUG'))...</span></code></div> </div> </div> <div class="note" id="94953"> <div class="votes"> <div id="Vu94953"> <a href="/manual/vote-note.php?id=94953&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd94953"> <a href="/manual/vote-note.php?id=94953&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V94953" title="65% like this..."> 25 </div> </div> <a href="#94953" class="name"> <strong class="user"><em>r dot hartung at roberthartung dot de</em></strong></a><a class="genanchor" href="#94953"> ¶</a><div class="date" title="2009-12-03 11:45"><strong>15 years ago</strong></div> <div class="text" id="Hcom94953"> <div class="phpcode"><code><span class="html">You can use the late static command "static::" withing defined as well. This example outputs - as expected - "int (2)" <br /> <br /><span class="default"><?php <br /> </span><span class="keyword">abstract class </span><span class="default">class1 <br /> </span><span class="keyword">{ <br /> public function </span><span class="default">getConst</span><span class="keyword">() <br /> { <br /> return </span><span class="default">defined</span><span class="keyword">(</span><span class="string">'static::SOME_CONST'</span><span class="keyword">) ? static::</span><span class="default">SOME_CONST </span><span class="keyword">: </span><span class="default">false</span><span class="keyword">; <br /> } <br /> } <br /> <br /> final class </span><span class="default">class2 </span><span class="keyword">extends </span><span class="default">class1 <br /> </span><span class="keyword">{ <br /> const </span><span class="default">SOME_CONST </span><span class="keyword">= </span><span class="default">2</span><span class="keyword">; <br /> } <br /> <br /> </span><span class="default">$class2 </span><span class="keyword">= new </span><span class="default">class2</span><span class="keyword">; <br /> <br /> </span><span class="default">var_dump</span><span class="keyword">(</span><span class="default">$class2</span><span class="keyword">-></span><span class="default">getConst</span><span class="keyword">()); <br /></span><span class="default">?></span></span></code></div> </div> </div> <div class="note" id="123239"> <div class="votes"> <div id="Vu123239"> <a href="/manual/vote-note.php?id=123239&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd123239"> <a href="/manual/vote-note.php?id=123239&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V123239" title="69% like this..."> 9 </div> </div> <a href="#123239" class="name"> <strong class="user"><em>louis at louisworks dot de</em></strong></a><a class="genanchor" href="#123239"> ¶</a><div class="date" title="2018-10-16 01:06"><strong>6 years ago</strong></div> <div class="text" id="Hcom123239"> <div class="phpcode"><code><span class="html">Dont forget to put the name of your constant into single quotation mark. You will not get an error or a warning.<br /><br /><span class="default"><?php<br />define</span><span class="keyword">(</span><span class="string">"AMOUNT_OF_APPLES"</span><span class="keyword">, </span><span class="default">12</span><span class="keyword">);<br />if(</span><span class="default">defined</span><span class="keyword">(</span><span class="default">AMOUNT_OF_APPLES</span><span class="keyword">)){<br /> </span><span class="comment">//you won't get an output here<br /> </span><span class="keyword">echo </span><span class="default">AMOUNT_OF_APPLES</span><span class="keyword">;<br />}<br /></span><span class="default">?><br /></span><br />so do instead<br /><br /><span class="default"><?php<br />define</span><span class="keyword">(</span><span class="string">"AMOUNT_OF_APPLES"</span><span class="keyword">, </span><span class="default">12</span><span class="keyword">);<br />if(</span><span class="default">defined</span><span class="keyword">(</span><span class="string">"AMOUNT_OF_APPLES"</span><span class="keyword">)){<br /> </span><span class="comment">//here you go<br /> </span><span class="keyword">echo </span><span class="default">AMOUNT_OF_APPLES</span><span class="keyword">;<br />}<br /><br /></span><span class="comment">//output: 12<br /></span><span class="default">?><br /></span><br />It took me half an day to see it...</span></code></div> </div> </div> <div class="note" id="82148"> <div class="votes"> <div id="Vu82148"> <a href="/manual/vote-note.php?id=82148&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd82148"> <a href="/manual/vote-note.php?id=82148&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V82148" title="67% like this..."> 12 </div> </div> <a href="#82148" class="name"> <strong class="user"><em>Shaun H</em></strong></a><a class="genanchor" href="#82148"> ¶</a><div class="date" title="2008-03-28 11:30"><strong>17 years ago</strong></div> <div class="text" id="Hcom82148"> <div class="phpcode"><code><span class="html">I saw that PHP doesn't have an enum function so I created my own. It's not necessary, but can come in handy from time to time.<br /><br /><span class="default"><?php<br /> </span><span class="keyword">function </span><span class="default">enum</span><span class="keyword">()<br /> {<br /> </span><span class="default">$args </span><span class="keyword">= </span><span class="default">func_get_args</span><span class="keyword">();<br /> foreach(</span><span class="default">$args </span><span class="keyword">as </span><span class="default">$key</span><span class="keyword">=></span><span class="default">$arg</span><span class="keyword">)<br /> {<br /> if(</span><span class="default">defined</span><span class="keyword">(</span><span class="default">$arg</span><span class="keyword">))<br /> {<br /> die(</span><span class="string">'Redefinition of defined constant ' </span><span class="keyword">. </span><span class="default">$arg</span><span class="keyword">);<br /> }<br /><br /> </span><span class="default">define</span><span class="keyword">(</span><span class="default">$arg</span><span class="keyword">, </span><span class="default">$key</span><span class="keyword">);<br /> }<br /> }<br /> <br /> </span><span class="default">enum</span><span class="keyword">(</span><span class="string">'ONE'</span><span class="keyword">,</span><span class="string">'TWO'</span><span class="keyword">,</span><span class="string">'THREE'</span><span class="keyword">);<br /> echo </span><span class="default">ONE</span><span class="keyword">, </span><span class="string">' '</span><span class="keyword">, </span><span class="default">TWO</span><span class="keyword">, </span><span class="string">' '</span><span class="keyword">, </span><span class="default">THREE</span><span class="keyword">;<br /></span><span class="default">?></span></span></code></div> </div> </div> <div class="note" id="110530"> <div class="votes"> <div id="Vu110530"> <a href="/manual/vote-note.php?id=110530&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd110530"> <a href="/manual/vote-note.php?id=110530&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V110530" title="66% like this..."> 11 </div> </div> <a href="#110530" class="name"> <strong class="user"><em>passerbyxp at gmail dot com</em></strong></a><a class="genanchor" href="#110530"> ¶</a><div class="date" title="2012-11-02 12:10"><strong>12 years ago</strong></div> <div class="text" id="Hcom110530"> <div class="phpcode"><code><span class="html">This function, along with constant(), is namespace sensitive. And it might help if you imagine them always running under the "root namespace":<br /><br /><span class="default"><?php<br /></span><span class="keyword">namespace </span><span class="default">FOO\BAR<br /></span><span class="keyword">{<br /> const </span><span class="default">WMP</span><span class="keyword">=</span><span class="string">"wmp"</span><span class="keyword">;<br /> function </span><span class="default">test</span><span class="keyword">()<br /> {<br /> if(</span><span class="default">defined</span><span class="keyword">(</span><span class="string">"WMP"</span><span class="keyword">)) echo </span><span class="string">"direct: "</span><span class="keyword">.</span><span class="default">constant</span><span class="keyword">(</span><span class="string">"WMP"</span><span class="keyword">); </span><span class="comment">//doesn't work;<br /> </span><span class="keyword">elseif(</span><span class="default">defined</span><span class="keyword">(</span><span class="string">"FOO\\BAR\\WMP"</span><span class="keyword">)) echo </span><span class="string">"namespace: "</span><span class="keyword">.</span><span class="default">constant</span><span class="keyword">(</span><span class="string">"FOO\\BAR\\WMP"</span><span class="keyword">); </span><span class="comment">//works<br /> </span><span class="keyword">echo </span><span class="default">WMP</span><span class="keyword">; </span><span class="comment">//works<br /> </span><span class="keyword">}<br />}<br />namespace<br />{<br /> </span><span class="default">\FOO\BAR\test</span><span class="keyword">();<br />}</span></span></code></div> </div> </div> <div class="note" id="49442"> <div class="votes"> <div id="Vu49442"> <a href="/manual/vote-note.php?id=49442&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd49442"> <a href="/manual/vote-note.php?id=49442&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V49442" title="58% like this..."> 5 </div> </div> <a href="#49442" class="name"> <strong class="user"><em>ndove at cox dot net</em></strong></a><a class="genanchor" href="#49442"> ¶</a><div class="date" title="2005-01-27 07:20"><strong>20 years ago</strong></div> <div class="text" id="Hcom49442"> <div class="phpcode"><code><span class="html">In PHP5, you can actually use defined() to see if an object constant has been defined, like so:<br /><br /><span class="default"><?php<br /><br /></span><span class="keyword">class </span><span class="default">Generic<br /></span><span class="keyword">{<br /> const </span><span class="default">WhatAmI </span><span class="keyword">= </span><span class="string">'Generic'</span><span class="keyword">;<br />}<br /><br />if (</span><span class="default">defined</span><span class="keyword">(</span><span class="string">'Generic::WhatAmI'</span><span class="keyword">))<br />{<br /> echo </span><span class="default">Generic</span><span class="keyword">::</span><span class="default">WhatAmI</span><span class="keyword">;<br />}<br /><br /></span><span class="default">?><br /></span><br />Thought it may be useful to note.<br /><br />-Nick</span></code></div> </div> </div> <div class="note" id="92155"> <div class="votes"> <div id="Vu92155"> <a href="/manual/vote-note.php?id=92155&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd92155"> <a href="/manual/vote-note.php?id=92155&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V92155" title="57% like this..."> 4 </div> </div> <a href="#92155" class="name"> <strong class="user"><em>info at daniel-marschall dot de</em></strong></a><a class="genanchor" href="#92155"> ¶</a><div class="date" title="2009-07-11 01:51"><strong>15 years ago</strong></div> <div class="text" id="Hcom92155"> <div class="phpcode"><code><span class="html">I found something out: defined() becomes probably false if a reference gets lost.<br /><br /><span class="default"><?php<br /><br />session_start</span><span class="keyword">(); </span><span class="comment">// $_SESSION created<br /></span><span class="default">define</span><span class="keyword">(</span><span class="string">'SESSION_BACKUP'</span><span class="keyword">, </span><span class="default">$_SESSION</span><span class="keyword">);<br />if (</span><span class="default">defined</span><span class="keyword">(</span><span class="string">'SESSION_BACKUP'</span><span class="keyword">)) echo </span><span class="string">'A'</span><span class="keyword">;<br /></span><span class="default">session_unset</span><span class="keyword">(); </span><span class="comment">// $_SESSION destroyed<br /></span><span class="keyword">if (</span><span class="default">defined</span><span class="keyword">(</span><span class="string">'SESSION_BACKUP'</span><span class="keyword">)) echo </span><span class="string">'B'</span><span class="keyword">;<br /><br /></span><span class="default">?><br /></span><br />You will see "A", but not "B".</span></code></div> </div> </div> <div class="note" id="125341"> <div class="votes"> <div id="Vu125341"> <a href="/manual/vote-note.php?id=125341&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd125341"> <a href="/manual/vote-note.php?id=125341&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V125341" title="55% like this..."> 1 </div> </div> <a href="#125341" class="name"> <strong class="user"><em>ohcc at 163 dot com</em></strong></a><a class="genanchor" href="#125341"> ¶</a><div class="date" title="2020-09-09 02:08"><strong>4 years ago</strong></div> <div class="text" id="Hcom125341"> <div class="phpcode"><code><span class="html">If a constant's name has a leading backslash (\), it's not possible to detect its existence using the defined() function, or to get its value using the constant() function. <br /><br />You can check its existence and get its value using the get_defined_constants() function, or prepend 2 more backslashes (\\) to the constant's name.<br /><br /><span class="default"><?php<br /> define</span><span class="keyword">(</span><span class="string">'\DOMAIN'</span><span class="keyword">, </span><span class="string">'wuxiancheng.cn'</span><span class="keyword">);<br /> </span><span class="default">$isDefined </span><span class="keyword">= </span><span class="default">defined</span><span class="keyword">(</span><span class="string">'\DOMAIN'</span><span class="keyword">); </span><span class="comment">// false<br /> </span><span class="default">$domain </span><span class="keyword">= </span><span class="default">constant</span><span class="keyword">(</span><span class="string">'\DOMAIN'</span><span class="keyword">); </span><span class="comment">// NULL, in Php 8+ you'll get a Fatal error.<br /> </span><span class="default">var_dump</span><span class="keyword">(</span><span class="default">$isDefined</span><span class="keyword">, </span><span class="default">$domain</span><span class="keyword">);<br /></span><span class="default">?><br /></span><br /><span class="default"><?php<br /> define</span><span class="keyword">(</span><span class="string">'\DOMAIN'</span><span class="keyword">, </span><span class="string">'wuxiancheng.cn'</span><span class="keyword">);<br /> </span><span class="default">$constants </span><span class="keyword">= </span><span class="default">get_defined_constants</span><span class="keyword">();<br /> </span><span class="default">$isDefined </span><span class="keyword">= isSet(</span><span class="default">$constants</span><span class="keyword">[</span><span class="string">'\DOMAIN'</span><span class="keyword">]);<br /> </span><span class="default">$domain </span><span class="keyword">= </span><span class="default">$isDefined </span><span class="keyword">? </span><span class="default">$constants</span><span class="keyword">[</span><span class="string">'\DOMAIN'</span><span class="keyword">] : </span><span class="default">NULL</span><span class="keyword">;<br /> </span><span class="default">var_dump</span><span class="keyword">(</span><span class="default">$isDefined</span><span class="keyword">, </span><span class="default">$domain</span><span class="keyword">);<br /></span><span class="default">?><br /></span><br /><span class="default"><?php<br /> define</span><span class="keyword">(</span><span class="string">'\DOMAIN'</span><span class="keyword">, </span><span class="string">'wuxiancheng.cn'</span><span class="keyword">);<br /> </span><span class="default">$isDefined </span><span class="keyword">= </span><span class="default">defined</span><span class="keyword">(</span><span class="string">'\\\DOMAIN'</span><span class="keyword">);<br /> </span><span class="default">$domain </span><span class="keyword">= </span><span class="default">constant</span><span class="keyword">(</span><span class="string">'\\\DOMAIN'</span><span class="keyword">);<br /> </span><span class="default">var_dump</span><span class="keyword">(</span><span class="default">$isDefined</span><span class="keyword">, </span><span class="default">$domain</span><span class="keyword">);<br /></span><span class="default">?></span></span></code></div> </div> </div> <div class="note" id="121433"> <div class="votes"> <div id="Vu121433"> <a href="/manual/vote-note.php?id=121433&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd121433"> <a href="/manual/vote-note.php?id=121433&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V121433" title="54% like this..."> 2 </div> </div> <a href="#121433" class="name"> <strong class="user"><em>Anonymous</em></strong></a><a class="genanchor" href="#121433"> ¶</a><div class="date" title="2017-07-26 06:39"><strong>7 years ago</strong></div> <div class="text" id="Hcom121433"> <div class="phpcode"><code><span class="html">Be careful with boolean defines and assuming a check is done for a specific value by defined such as<br /><span class="default"><?php<br /><br />define</span><span class="keyword">(</span><span class="string">'DEBUG'</span><span class="keyword">, </span><span class="default">false</span><span class="keyword">);<br /><br />if(</span><span class="default">defined</span><span class="keyword">(</span><span class="string">'DEBUG'</span><span class="keyword">)){<br /> echo </span><span class="string">'Not really debugging mode'</span><span class="keyword">;<br />}<br /></span><span class="default">?><br /></span><br />You want to also check the constant as in<br /><br /><span class="default"><?php<br />define</span><span class="keyword">(</span><span class="string">'DEBUG'</span><span class="keyword">, </span><span class="default">true</span><span class="keyword">);<br /><br />if(</span><span class="default">defined</span><span class="keyword">(</span><span class="string">'DEBUG'</span><span class="keyword">) && </span><span class="default">DEBUG</span><span class="keyword">){<br /> echo </span><span class="string">'Really this is debugging mode'</span><span class="keyword">;<br />}<br /></span><span class="default">?><br /></span><br />All defined is doing is verifying the constant exists not it's value.</span></code></div> </div> </div> <div class="note" id="99694"> <div class="votes"> <div id="Vu99694"> <a href="/manual/vote-note.php?id=99694&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd99694"> <a href="/manual/vote-note.php?id=99694&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V99694" title="53% like this..."> 3 </div> </div> <a href="#99694" class="name"> <strong class="user"><em>vindozo at gmail dot com</em></strong></a><a class="genanchor" href="#99694"> ¶</a><div class="date" title="2010-08-31 02:21"><strong>14 years ago</strong></div> <div class="text" id="Hcom99694"> <div class="phpcode"><code><span class="html">If you wish to protect files from direct access I normally use this:<br /><br />index.php:<br /><br /><span class="default"><?php<br /></span><span class="comment">// Main stuff here<br /></span><span class="default">define</span><span class="keyword">(</span><span class="string">'START'</span><span class="keyword">,</span><span class="default">microtime</span><span class="keyword">());<br /><br />include </span><span class="string">"x.php"</span><span class="keyword">;<br /></span><span class="default">?><br /></span><br />x.php:<br /><br /><span class="default"><?php<br />defined</span><span class="keyword">(</span><span class="string">'START'</span><span class="keyword">)||(</span><span class="default">header</span><span class="keyword">(</span><span class="string">"HTTP/1.1 403 Forbidden"</span><span class="keyword">)&die(</span><span class="string">'403.14 - Directory listing denied.'</span><span class="keyword">));<br /></span><span class="default">?></span></span></code></div> </div> </div> <div class="note" id="129418"> <div class="votes"> <div id="Vu129418"> <a href="/manual/vote-note.php?id=129418&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd129418"> <a href="/manual/vote-note.php?id=129418&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V129418" title="0% like this..."> -1 </div> </div> <a href="#129418" class="name"> <strong class="user"><em>Pieter Frenssen</em></strong></a><a class="genanchor" href="#129418"> ¶</a><div class="date" title="2024-04-19 08:54"><strong>11 months ago</strong></div> <div class="text" id="Hcom129418"> <div class="phpcode"><code><span class="html">To check in a trait if a constant is defined in the class using the trait, prepend the constant name with `self::class`:<br /><br /><span class="default"><?php<br /><br /></span><span class="keyword">trait </span><span class="default">MyTrait </span><span class="keyword">{<br /> public function </span><span class="default">checkConstant</span><span class="keyword">() {<br /> </span><span class="default">assert</span><span class="keyword">(</span><span class="default">defined</span><span class="keyword">(</span><span class="default">self</span><span class="keyword">::class . </span><span class="string">"::MY_CONSTANT"</span><span class="keyword">));<br /> print </span><span class="default">self</span><span class="keyword">::</span><span class="default">MY_CONSTANT</span><span class="keyword">;<br /> }<br />}<br /><br />class </span><span class="default">MyClass </span><span class="keyword">{<br /> use </span><span class="default">MyTrait</span><span class="keyword">;<br /> protected const </span><span class="default">MY_CONSTANT </span><span class="keyword">= </span><span class="string">'my value'</span><span class="keyword">;<br />}<br /><br /></span><span class="default">$class </span><span class="keyword">= new </span><span class="default">MyClass</span><span class="keyword">();<br /></span><span class="default">$class</span><span class="keyword">-></span><span class="default">checkConstant</span><span class="keyword">();<br /></span><span class="default">?></span></span></code></div> </div> </div> <div class="note" id="87340"> <div class="votes"> <div id="Vu87340"> <a href="/manual/vote-note.php?id=87340&page=function.defined&vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd87340"> <a href="/manual/vote-note.php?id=87340&page=function.defined&vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V87340" title="51% like this..."> 1 </div> </div> <a href="#87340" class="name"> <strong class="user"><em>reachmike at hotpop dot com</em></strong></a><a class="genanchor" href="#87340"> ¶</a><div class="date" title="2008-11-30 10:17"><strong>16 years ago</strong></div> <div class="text" id="Hcom87340"> <div class="phpcode"><code><span class="html">You may find that if you use <span class="default"><?= ?></span> to dump your constants, and they are not defined, depending on your error reporting level, you may not display an error and, instead, just show the name of the constant. For example:<br /><br /><span class="default"><?= TEST ?><br /></span><br />...may say TEST instead of an empty string like you might expect. The fix is a function like this:<br /><br /><span class="default"><?php<br /><br /></span><span class="keyword">function </span><span class="default">C</span><span class="keyword">(&</span><span class="default">$constant</span><span class="keyword">) {<br /> </span><span class="default">$nPrev1 </span><span class="keyword">= </span><span class="default">error_reporting</span><span class="keyword">(</span><span class="default">E_ALL</span><span class="keyword">);<br /> </span><span class="default">$sPrev2 </span><span class="keyword">= </span><span class="default">ini_set</span><span class="keyword">(</span><span class="string">'display_errors'</span><span class="keyword">, </span><span class="string">'0'</span><span class="keyword">);<br /> </span><span class="default">$sTest </span><span class="keyword">= </span><span class="default">defined</span><span class="keyword">(</span><span class="default">$constant</span><span class="keyword">) ? </span><span class="string">'defined' </span><span class="keyword">: </span><span class="string">'not defined'</span><span class="keyword">;<br /> </span><span class="default">$oTest </span><span class="keyword">= (object) </span><span class="default">error_get_last</span><span class="keyword">();<br /> </span><span class="default">error_reporting</span><span class="keyword">(</span><span class="default">$nPrev1</span><span class="keyword">);<br /> </span><span class="default">ini_set</span><span class="keyword">(</span><span class="string">'display_errors'</span><span class="keyword">, </span><span class="default">$sPrev2</span><span class="keyword">);<br /> if (</span><span class="default">$oTest</span><span class="keyword">-></span><span class="default">message</span><span class="keyword">) {<br /> return </span><span class="string">''</span><span class="keyword">;<br /> } else {<br /> return </span><span class="default">$constant</span><span class="keyword">;<br /> }<br />}<br /><br /></span><span class="default">?><br /></span><br />And so now you can do:<br /><br /><span class="default"><?= C</span><span class="keyword">(</span><span class="default">TEST</span><span class="keyword">) </span><span class="default">?><br /></span><br />If TEST was assigned with define(), then you'll receive the value. If not, then you'll receive an empty string.<br /><br />Please post if you can do this in fewer lines of code or do something more optimal than toggling the error handler.</span></code></div> </div> </div></div> <div class="foot"><a href="/manual/add-note.php?sect=function.defined&repo=en&redirect=https://www.php.net/manual/en/function.defined.php">+<small>add a note</small></a></div> </section> </section><!-- layout-content --> <aside class='layout-menu'> <ul class='parent-menu-list'> <li> <a href="ref.misc.php">Misc. Functions</a> <ul class='child-menu-list'> <li class=""> <a href="function.connection-aborted.php" title="connection_​aborted">connection_​aborted</a> </li> <li class=""> <a href="function.connection-status.php" title="connection_​status">connection_​status</a> </li> <li class=""> <a href="function.constant.php" title="constant">constant</a> </li> <li class=""> <a href="function.define.php" title="define">define</a> </li> <li class="current"> <a href="function.defined.php" title="defined">defined</a> </li> <li class=""> <a href="function.die.php" title="die">die</a> </li> <li class=""> <a href="function.eval.php" title="eval">eval</a> </li> <li class=""> <a href="function.exit.php" title="exit">exit</a> </li> <li class=""> <a href="function.get-browser.php" title="get_​browser">get_​browser</a> </li> <li class=""> <a href="function.halt-compiler.php" title="_​_​halt_​compiler">_​_​halt_​compiler</a> </li> <li class=""> <a href="function.highlight-file.php" title="highlight_​file">highlight_​file</a> </li> <li class=""> <a href="function.highlight-string.php" title="highlight_​string">highlight_​string</a> </li> <li class=""> <a href="function.hrtime.php" title="hrtime">hrtime</a> </li> <li class=""> <a href="function.ignore-user-abort.php" title="ignore_​user_​abort">ignore_​user_​abort</a> </li> <li class=""> <a href="function.pack.php" title="pack">pack</a> </li> <li class=""> <a href="function.php-strip-whitespace.php" title="php_​strip_​whitespace">php_​strip_​whitespace</a> </li> <li class=""> <a href="function.sapi-windows-cp-conv.php" title="sapi_​windows_​cp_​conv">sapi_​windows_​cp_​conv</a> </li> <li class=""> <a href="function.sapi-windows-cp-get.php" title="sapi_​windows_​cp_​get">sapi_​windows_​cp_​get</a> </li> <li class=""> <a href="function.sapi-windows-cp-is-utf8.php" title="sapi_​windows_​cp_​is_​utf8">sapi_​windows_​cp_​is_​utf8</a> </li> <li class=""> <a href="function.sapi-windows-cp-set.php" title="sapi_​windows_​cp_​set">sapi_​windows_​cp_​set</a> </li> <li class=""> <a href="function.sapi-windows-generate-ctrl-event.php" title="sapi_​windows_​generate_​ctrl_​event">sapi_​windows_​generate_​ctrl_​event</a> </li> <li class=""> <a href="function.sapi-windows-set-ctrl-handler.php" title="sapi_​windows_​set_​ctrl_​handler">sapi_​windows_​set_​ctrl_​handler</a> </li> <li class=""> <a href="function.sapi-windows-vt100-support.php" title="sapi_​windows_​vt100_​support">sapi_​windows_​vt100_​support</a> </li> <li class=""> <a href="function.show-source.php" title="show_​source">show_​source</a> </li> <li class=""> <a href="function.sleep.php" title="sleep">sleep</a> </li> <li class=""> <a href="function.sys-getloadavg.php" title="sys_​getloadavg">sys_​getloadavg</a> </li> <li class=""> <a href="function.time-nanosleep.php" title="time_​nanosleep">time_​nanosleep</a> </li> <li class=""> <a href="function.time-sleep-until.php" title="time_​sleep_​until">time_​sleep_​until</a> </li> <li class=""> <a href="function.uniqid.php" title="uniqid">uniqid</a> </li> <li class=""> <a href="function.unpack.php" title="unpack">unpack</a> </li> <li class=""> <a href="function.usleep.php" title="usleep">usleep</a> </li> </ul> </li> </ul> </aside> </div><!-- layout --> <footer> <div class="container footer-content"> <div class="row-fluid"> <ul class="footmenu"> <li><a href="/manual/en/copyright.php">Copyright © 2001-2025 The PHP Documentation Group</a></li> <li><a href="/my.php">My PHP.net</a></li> <li><a href="/contact.php">Contact</a></li> <li><a href="/sites.php">Other PHP.net sites</a></li> <li><a href="/privacy.php">Privacy policy</a></li> </ul> </div> </div> </footer> <script src="/cached.php?t=1731172202&f=/js/ext/jquery-3.6.0.min.js"></script> <script src="/cached.php?t=1723177202&f=/js/ext/FuzzySearch.min.js"></script> <script src="/cached.php?t=1707321815&f=/js/ext/mousetrap.min.js"></script> <script src="/cached.php?t=1707321815&f=/js/ext/jquery.scrollTo.min.js"></script> <script src="/cached.php?t=1738028401&f=/js/search.js"></script> <script src="/cached.php?t=1738742401&f=/js/common.js"></script> <script type="module" src="/cached.php?t=1741309802&f=/js/interactive-examples.js"></script> <a id="toTop" href="javascript:;"><span id="toTopHover"></span><img width="40" height="40" alt="To Top" src="/images/to-top@2x.png"></a> <div id="search-modal__backdrop" class="search-modal__backdrop"> <div role="dialog" aria-label="Search modal" id="search-modal" class="search-modal" > <div class="search-modal__header"> <div class="search-modal__form"> <div class="search-modal__input-icon"> <!-- https://feathericons.com search --> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </div> <input type="search" id="search-modal__input" class="search-modal__input" placeholder="Search docs" aria-label="Search docs" /> </div> <button aria-label="Close" class="search-modal__close"> <!-- https://pictogrammers.com/library/mdi/icon/close/ --> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" > <path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z"/> </svg> </button> </div> <div role="listbox" aria-label="Search results" id="search-modal__results" class="search-modal__results" ></div> <div class="search-modal__helper-text"> <div> <kbd>↑</kbd> and <kbd>↓</kbd> to navigate • <kbd>Enter</kbd> to select • <kbd>Esc</kbd> to close </div> <div> Press <kbd>Enter</kbd> without selection to search using Google </div> </div> </div> </div> </body> </html>