{"id":159,"date":"2021-03-08T03:27:59","date_gmt":"2021-03-08T03:27:59","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=159"},"modified":"2025-04-07T12:38:54","modified_gmt":"2025-04-07T12:38:54","slug":"php-magic-constants","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-magic-constants\/","title":{"rendered":"PHP Magic Constants"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about PHP magic constants that hold values change depending on where they are used.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-magic-constants'>Introduction to the PHP magic constants <a href=\"#introduction-to-the-php-magic-constants\" class=\"anchor\" id=\"introduction-to-the-php-magic-constants\" title=\"Anchor for Introduction to the PHP magic constants\">#<\/a><\/h2>\n\n\n\n<p>In PHP, regular <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-constants\/\">constants<\/a> store values that don&#8217;t change during the execution of the script. PHP resolves the constants defined by the <code>define()<\/code> function at runtime and resolves the constants defined by the <code>const<\/code> keyword at compile time. <\/p>\n\n\n\n<p>Besides the regular constants, PHP has some constants whose values change depending on where you use them. For example, the <code>__LINE__<\/code> constant returns the current line number of the current script. <\/p>\n\n\n\n<p>These constants are known as magic constants. Unlike regular constants, PHP resolves the magic constants at runtime.<\/p>\n\n\n\n<p>The following table illustrates the magic constants in PHP:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Name<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>__LINE__<\/code><\/td><td>Return the current line number of the file.<\/td><\/tr><tr><td><code>__FILE__<\/code><\/td><td>Return the full path and filename of the file or <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-include-file\/\">include<\/a>.<\/td><\/tr><tr><td><code>__DIR__<\/code><\/td><td>Return the directory of the file or include. It&#8217;s equivalent to&nbsp;<code>dirname(__FILE__)<\/code>. The __DIR__ doesn&#8217;t have a trailing slash (<code>\/<\/code>) unless it&#8217;s the root directory.<\/td><\/tr><tr><td><code>__FUNCTION__<\/code><\/td><td>Return function name, or&nbsp;<code>{closure}<\/code>&nbsp;for <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-anonymous-functions\/\">anonymous functions<\/a>.<\/td><\/tr><tr><td><code>__CLASS__<\/code><\/td><td>Return the <a href=\"https:\/\/phptutorial.net\/php-oop\/php-objects\/\">class name<\/a> that also includes the namespace.<\/td><\/tr><tr><td><code>__TRAIT__<\/code><\/td><td>Return the <a href=\"https:\/\/phptutorial.net\/php-oop\/php-traits\/\">trait<\/a> name that includes the namespace<\/td><\/tr><tr><td><code>__METHOD__<\/code><\/td><td>Return the name of the current instance method.<\/td><\/tr><tr><td><code>__NAMESPACE__<\/code><\/td><td>Return the name of the current <a href=\"https:\/\/phptutorial.net\/php-oop\/php-namespace\/\">namespace<\/a>.<\/td><\/tr><tr><td><code>ClassName::class<\/code><\/td><td>Return the fully qualified class name.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-magic-constant-example'>PHP magic constant example <a href=\"#php-magic-constant-example\" class=\"anchor\" id=\"php-magic-constant-example\" title=\"Anchor for PHP magic constant example\">#<\/a><\/h2>\n\n\n\n<p>The following example shows the values of the above magic constants:<\/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\">namespace<\/span> <span class=\"hljs-title\">App<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">HttpRequest<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">()<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The current class name is '<\/span> .  <span class=\"hljs-keyword\">__CLASS__<\/span> . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;\n    }\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">isPost<\/span><span class=\"hljs-params\">()<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The current method is '<\/span> . <span class=\"hljs-keyword\">__METHOD__<\/span> . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;\n        \n        <span class=\"hljs-keyword\">return<\/span> $_SERVER&#91;<span class=\"hljs-string\">'REQUEST_METHOD'<\/span>];\n    }\n}\n\n$request = <span class=\"hljs-keyword\">new<\/span> HttpRequest();\n\n$request-&gt;isPost();\n\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The current namespace is '<\/span> . <span class=\"hljs-keyword\">__NAMESPACE__<\/span> . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The fully qualified class name is '<\/span>  . HttpRequest::class . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The current file is '<\/span> . <span class=\"hljs-keyword\">__FILE__<\/span> . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The current line is '<\/span> . <span class=\"hljs-keyword\">__LINE__<\/span> . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The current directory is '<\/span> . <span class=\"hljs-keyword\">__DIR__<\/span>  . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;<\/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>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">The current class name is App\\HttpRequest\nThe current method is App\\HttpRequest::isPost\nThe current namespace is App\nThe fully qualified class name is App\\HttpRequest\nThe current file is C:\\xampp\\htdocs\\phptutorial\\class.php\nThe current line is 29\nThe current directory is C:\\xampp\\htdocs\\phptutorial\\<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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\"><li>PHP magic constants hold values resolved at runtime, not compile time.<\/li><li>The values of the magic constants change depending on where they&#8217;re used.<\/li><\/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=\"159\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-magic-constants\/\"\n\t\t\t\tdata-post-title=\"PHP Magic Constants\"\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=\"159\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-magic-constants\/\"\n\t\t\t\tdata-post-title=\"PHP Magic Constants\"\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 about PHP magic constants that hold values change depending on where they are used.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":161,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-159","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/159","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=159"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/159\/revisions"}],"predecessor-version":[{"id":2393,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/159\/revisions\/2393"}],"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=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}