{"id":147,"date":"2021-03-08T02:25:08","date_gmt":"2021-03-08T02:25:08","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=147"},"modified":"2025-04-06T02:18:38","modified_gmt":"2025-04-06T02:18:38","slug":"php-variable-scopes","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-variable-scopes\/","title":{"rendered":"PHP Variable Scopes"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about PHP variable scopes, which specify the part of code that can access a variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-php-variable-scopes'>Introduction to PHP variable scopes <a href=\"#introduction-to-php-variable-scopes\" class=\"anchor\" id=\"introduction-to-php-variable-scopes\" title=\"Anchor for Introduction to PHP variable scopes\">#<\/a><\/h2>\n\n\n\n<p>The scope of a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">variable<\/a> determines which part of the code can access it. The locations where the variable can be accessible determine the scope of the variable.<\/p>\n\n\n\n<p>In PHP, variables have four types of scopes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Local<\/li>\n\n\n\n<li>Global<\/li>\n\n\n\n<li>Static<\/li>\n\n\n\n<li>Function parameters<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='local-variables'>Local variables <a href=\"#local-variables\" class=\"anchor\" id=\"local-variables\" title=\"Anchor for Local variables\">#<\/a><\/h2>\n\n\n\n<p>When you <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">define a variable<\/a> inside a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-functions\/\">function<\/a>, you can only access that variable within the function. The variable is said to be local to the function.<\/p>\n\n\n\n<p>The following example defines the <code>say()<\/code> function that displays the <code>'Hi'<\/code> message:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">say<\/span><span class=\"hljs-params\">()<\/span>\n<\/span>{\n\t$message = <span class=\"hljs-string\">'Hi'<\/span>;\n\t<span class=\"hljs-keyword\">echo<\/span> $message;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Inside the <code>say()<\/code> function, we define the <code>$message<\/code> variable. The <code>$message<\/code> variable is a local variable. And you cannot access it from the outside of the <code>say()<\/code> function.<\/p>\n\n\n\n<p>Also, the <code>$message<\/code> variable only exists during the execution of the <code>say()<\/code> function. Once the <code>say()<\/code> function ends, the <code>$mesage<\/code> variable won&#8217;t exist anymore.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='global-variables'>Global variables <a href=\"#global-variables\" class=\"anchor\" id=\"global-variables\" title=\"Anchor for Global variables\">#<\/a><\/h2>\n\n\n\n<p>When you declare a variable outside of a function, the variable is global. You can access the variable anywhere within the script except inside a function. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$message = <span class=\"hljs-string\">'Hello'<\/span>;\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">say<\/span><span class=\"hljs-params\">()<\/span>\n<\/span>{\n\t$message = <span class=\"hljs-string\">'Hi'<\/span>;\n\t<span class=\"hljs-keyword\">echo<\/span> $message;\n}\n\n<span class=\"hljs-keyword\">echo<\/span> $message; <span class=\"hljs-comment\">\/\/ Hello<\/span><\/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><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRtZXNzYWdlID0gJ0hlbGxvJzsKCmZ1bmN0aW9uIHNheSgpCnsKCSRtZXNzYWdlID0gJ0hpJzsKCWVjaG8gJG1lc3NhZ2U7Cn0KCmVjaG8gJG1lc3NhZ2U7IC8vIEhlbGxv\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this script, we have two variables with the same name <code>$message<\/code>.<\/p>\n\n\n\n<p>The first is the global variable because we define it outside a function. The <code>$message<\/code> variable that we define inside the function is the local variable. Even though these variables have the same name, they&#8217;re different.<\/p>\n\n\n\n<p>PHP allows you to access a global variable within a function by using the <code>global<\/code> keyword. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$message = <span class=\"hljs-string\">'Hello'<\/span>;\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">say<\/span><span class=\"hljs-params\">()<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">global<\/span> $message;\n\t<span class=\"hljs-keyword\">echo<\/span> $message; <span class=\"hljs-comment\">\/\/ Hello<\/span>\n}\n\nsay();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRtZXNzYWdlID0gJ0hlbGxvJzsKCmZ1bmN0aW9uIHNheSgpCnsKCWdsb2JhbCAkbWVzc2FnZTsKCWVjaG8gJG1lc3NhZ2U7IC8vIEhlbGxvCn0KCnNheSgpOw\" 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 a global variable called <code>$message<\/code>.<\/li>\n\n\n\n<li>Second, reference the global variable <code>$message<\/code> inside the <code>say()<\/code> function.<\/li>\n<\/ul>\n\n\n\n<p class=\"warning\">It&#8217;s important to note that using global variables is not a good practice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='superglobal-variables'>Superglobal variables <a href=\"#superglobal-variables\" class=\"anchor\" id=\"superglobal-variables\" title=\"Anchor for Superglobal variables\">#<\/a><\/h3>\n\n\n\n<p>PHP has a list of built-in variables known as superglobal variables. These variables provide information about the PHP script&#8217;s environment.<\/p>\n\n\n\n<p>The superglobal variables are always available in all parts of the script. The following table shows the list of &nbsp;PHP superglobal variables:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Superglobal Variables<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td>$GLOBALS<\/td><td>Returns an array that contains global variables. The variable names select which part of the array to access.<\/td><\/tr><tr><td>$_SERVER<\/td><td>Returns data about the web server environment.<\/td><\/tr><tr><td>$_GET<\/td><td>Return data from <code>GET<\/code> requests.<\/td><\/tr><tr><td>$_POST<\/td><td>Return data from <code>POST<\/code> requests.<\/td><\/tr><tr><td>$_COOKIE<\/td><td>Return data from HTTP cookies<\/td><\/tr><tr><td>$_FILES<\/td><td>Return data from POST file uploads.<\/td><\/tr><tr><td>$_ENV<\/td><td>Return information about the script&#8217;s environment.<\/td><\/tr><tr><td>$_REQUEST<\/td><td>Return data from the HTTP request<\/td><\/tr><tr><td>$_SESSION<\/td><td>Return variables registered in a session<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='static-variables'>Static variables <a href=\"#static-variables\" class=\"anchor\" id=\"static-variables\" title=\"Anchor for Static variables\">#<\/a><\/h2>\n\n\n\n<p>A static variable retains its value between function calls. Also, a static variable is only accessible inside the function. To define a static variable, you use the&nbsp;<code>static<\/code> keyword. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">get_counter<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    <span class=\"hljs-keyword\">static<\/span> $counter = <span class=\"hljs-number\">1<\/span>;\n    <span class=\"hljs-keyword\">return<\/span> $counter++;\n}\n\n<span class=\"hljs-keyword\">echo<\/span> get_counter() .  <span class=\"hljs-string\">'&lt;br&gt;'<\/span>; <span class=\"hljs-comment\">\/\/ 1<\/span>\n<span class=\"hljs-keyword\">echo<\/span> get_counter() .  <span class=\"hljs-string\">'&lt;br&gt;'<\/span>; <span class=\"hljs-comment\">\/\/ 2<\/span>\n<span class=\"hljs-keyword\">echo<\/span> get_counter() .  <span class=\"hljs-string\">'&lt;br&gt;'<\/span>; <span class=\"hljs-comment\">\/\/ 3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmZ1bmN0aW9uIGdldF9jb3VudGVyKCkgewogICAgc3RhdGljICRjb3VudGVyID0gMTsKICAgIHJldHVybiAkY291bnRlcisrOwp9CgplY2hvIGdldF9jb3VudGVyKCkgLiAgJzxicj4nOyAvLyAxCmVjaG8gZ2V0X2NvdW50ZXIoKSAuICAnPGJyPic7IC8vIDIKZWNobyBnZXRfY291bnRlcigpIC4gICc8YnI-JzsgLy8gMw\" 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-5\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">1\n2\n3<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define the <code>get_counter()<\/code>\u00a0function with a static variable named <code>$counter<\/code>.\u00a0<\/li>\n\n\n\n<li>Second, call the <code>set_counter()<\/code> function three times. As you notice the value of the <code>$counter<\/code> variable is increased by one after each function call.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='function-parameters'>Function parameters <a href=\"#function-parameters\" class=\"anchor\" id=\"function-parameters\" title=\"Anchor for Function parameters\">#<\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-function-parameters\/\">Function parameters<\/a> are local to the function. Therefore, function parameters can only be accessible inside the function. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">sum<\/span><span class=\"hljs-params\">($items)<\/span> <\/span>{\n    $total = <span class=\"hljs-number\">0<\/span>;\n    <span class=\"hljs-keyword\">foreach<\/span>($items <span class=\"hljs-keyword\">as<\/span> $item) {\n        $total += $item;\n    }\n    <span class=\"hljs-keyword\">return<\/span> $total;\n}\n\n<span class=\"hljs-comment\">\/\/ $items cannot be accessible here<\/span>\n<span class=\"hljs-keyword\">echo<\/span> sum(&#91;<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">20<\/span>,<span class=\"hljs-number\">30<\/span>]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAgCgpmdW5jdGlvbiBzdW0oJGl0ZW1zKSB7CiAgICAkdG90YWwgPSAwOwogICAgZm9yZWFjaCgkaXRlbXMgYXMgJGl0ZW0pIHsKICAgICAgICAkdG90YWwgKz0gJGl0ZW07CiAgICB9CiAgICByZXR1cm4gJHRvdGFsOwp9CgovLyAkaXRlbXMgY2Fubm90IGJlIGFjY2Vzc2libGUgaGVyZQplY2hvIHN1bShbMTAsMjAsMzBdKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, the <code>$items<\/code> is the parameter of the <code>sum()<\/code> function. It can only be accessible within the <code>sum()<\/code> function.<\/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>PHP has four types of variable scopes including local, global, static, and function parameters.<\/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=\"147\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-variable-scopes\/\"\n\t\t\t\tdata-post-title=\"PHP Variable Scopes\"\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=\"147\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-variable-scopes\/\"\n\t\t\t\tdata-post-title=\"PHP Variable Scopes\"\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&#8217;ll learn about PHP variable scopes, which specify the part of code that can access a variable.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":37,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-147","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/147","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=147"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/147\/revisions"}],"predecessor-version":[{"id":3027,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/147\/revisions\/3027"}],"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=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}