{"id":219,"date":"2024-03-02T16:51:22","date_gmt":"2024-03-02T16:51:22","guid":{"rendered":"https:\/\/www.xopsschool.com\/tutorials\/?p=219"},"modified":"2024-03-02T16:52:17","modified_gmt":"2024-03-02T16:52:17","slug":"php-variables","status":"publish","type":"post","link":"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/","title":{"rendered":"PHP Variables"},"content":{"rendered":"\n<p> Variables are used to store information which can be referenced and manipulated within a computer program. These variables also play a vital role in labeling data with a descriptive name, which makes it easier for the users to understand the program clearly and impressively. Some programmers also consider the variables as containers which can hold information.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Scope of a variable<\/h2>\n\n\n\n<p>The scope of a variable can be defined as its extent in program. It can be accessed in the program. Easily explained, it is the portion of a program within which it is clearly visible and accessible.<\/p>\n\n\n\n<p><strong>PHP has three variable scopes<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Local variable<\/li>\n\n\n\n<li>Global variable<\/li>\n\n\n\n<li>Static variable<\/li>\n<\/ul>\n\n\n\n<p><strong>Local variable <\/strong><\/p>\n\n\n\n<p>These are the variables which are declared within a function. It is so named as it has scope only within that particular function. It is not able to access outside the particular function. Any declaration of a variable that you use outside the function with the similar name, as the one used within the function, becomes a completely different one.<\/p>\n\n\n\n<p><strong>Code <\/strong><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> &lt;?php\r\n\r\n$num = 60;\r\n\r\nFunction local_var()\r\n\r\n{\r\n\r\n\/\/This $num is local to this function\r\n\r\n\/\/the variable $num outside this function\r\n\r\n\/\/is a completely different variable\r\n\r\n$num = 50;\r\n\r\necho \u201clocal num = $num \\n\u201d;\r\n\r\n}\r\n\r\nlocal_var();\r\n\r\n\/\/$num outside function local_var() is a\r\n\r\n\/\/ completely different variable than that of\r\n\r\n\/\/ inside local_var()\r\n\r\necho \u201cVariable num outside local_var() is $num \\n\u201d;\r\n\r\n?><\/code><\/pre>\n\n\n\n<p><strong>output<\/strong><\/p>\n\n\n\n<p>Local num = 50<\/p>\n\n\n\n<p>Variable num outside local_var() is 60<\/p>\n\n\n\n<p><strong>Global variable<\/strong><\/p>\n\n\n\n<p>The variables that are declared outside a particular function are known as global variables. One can access directly outside that particular function. In order to access the variable within a function, one need to use the word \u201cglobal\u201d, in front of the variable to refer to the particular global variable.<br><\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\n$num = 20;\r\n\r\n\/\/ functionto demonstrate use of global variable\r\n\r\nFunction global_var()\r\n\r\n{\r\n\r\n\/\/we have to use global keyword before\r\n\r\n\/\/ the variable $num to access within\r\n\r\n\/\/ the function\r\n\r\nglobal $num;\r\n\r\necho \u201cvariable num inside function : $num \\n\u201d;\r\n\r\n}\r\n\r\nglobal_var();\r\n\r\necho \u201cVariable num outside function : $num \\n\u201d;\r\n\r\n?>\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<p>Variable num inside function : 20<\/p>\n\n\n\n<p>Variable num outside function : 20<\/p>\n\n\n\n<p><strong>Static variable<\/strong><\/p>\n\n\n\n<p>It is the characteristic of PHP to delete a variable. Once the variable completes its execution and the entire memory is freed, it is to be deleted. But sometimes, the variables need to be stored even after the function is executed. To conduct this, PHP developers use static keyword and the variables then are known as the static variables.<\/p>\n\n\n\n<p>Example<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\n\/\/ function to demonstrate static variables\r\n\r\nfunctionstatic_var()\r\n\r\n{\r\n\r\n\/\/ static variable\r\n\r\nstatic $num = 5;\r\n\r\n$sum = 2;\r\n\r\n$sum++;\r\n\r\n$num++;\r\n\r\necho $num, \u201c\\n\u201d;\r\n\r\necho $sum, \u201c\\n\u201d;\r\n\r\n}\r\n\r\n\/\/ first function call\r\n\r\nstatic_var();\r\n\r\n\/\/ second function call\r\n\r\nstatic_var();\r\n\r\n?>\n\n<strong>Output<\/strong> \n\n6\r\n\r\n3\r\n\r\n7\r\n\r\n3<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Variables are used to store information which can be referenced and manipulated within a computer program. These variables also play a vital role in labeling data with a descriptive name, which makes it easier for the users to understand the program clearly and impressively. Some programmers also consider the variables as containers which can hold &#8230; <a title=\"PHP Variables\" class=\"read-more\" href=\"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/\" aria-label=\"Read more about PHP Variables\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-what-is-variable-and-how-to-declare-php-variable"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP Variables - XOps Tutorials!!!<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Variables - XOps Tutorials!!!\" \/>\n<meta property=\"og:description\" content=\"Variables are used to store information which can be referenced and manipulated within a computer program. These variables also play a vital role in labeling data with a descriptive name, which makes it easier for the users to understand the program clearly and impressively. Some programmers also consider the variables as containers which can hold ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"XOps Tutorials!!!\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-02T16:51:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-02T16:52:17+00:00\" \/>\n<meta name=\"author\" content=\"ritik hansda\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"ritik hansda\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/\"},\"author\":{\"name\":\"ritik hansda\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/c0119ddd2d27ea9cd476e809c8294ba7\"},\"headline\":\"PHP Variables\",\"datePublished\":\"2024-03-02T16:51:22+00:00\",\"dateModified\":\"2024-03-02T16:52:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/\"},\"wordCount\":306,\"commentCount\":0,\"articleSection\":[\"What Is Variable And How To Declare Php Variable?\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/\",\"url\":\"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/\",\"name\":\"PHP Variables - XOps Tutorials!!!\",\"isPartOf\":{\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/#website\"},\"datePublished\":\"2024-03-02T16:51:22+00:00\",\"dateModified\":\"2024-03-02T16:52:17+00:00\",\"author\":{\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/c0119ddd2d27ea9cd476e809c8294ba7\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.xopsschool.com\/tutorials\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/#website\",\"url\":\"https:\/\/www.xopsschool.com\/tutorials\/\",\"name\":\"XOps Tutorials!!!\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.xopsschool.com\/tutorials\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/c0119ddd2d27ea9cd476e809c8294ba7\",\"name\":\"ritik hansda\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6fe0bc437410899c524975272622377e50e726108b1ad03f7b4488f734304fce?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6fe0bc437410899c524975272622377e50e726108b1ad03f7b4488f734304fce?s=96&d=mm&r=g\",\"caption\":\"ritik hansda\"},\"url\":\"https:\/\/www.xopsschool.com\/tutorials\/author\/ritikhansda\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Variables - XOps Tutorials!!!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/","og_locale":"en_US","og_type":"article","og_title":"PHP Variables - XOps Tutorials!!!","og_description":"Variables are used to store information which can be referenced and manipulated within a computer program. These variables also play a vital role in labeling data with a descriptive name, which makes it easier for the users to understand the program clearly and impressively. Some programmers also consider the variables as containers which can hold ... Read more","og_url":"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/","og_site_name":"XOps Tutorials!!!","article_published_time":"2024-03-02T16:51:22+00:00","article_modified_time":"2024-03-02T16:52:17+00:00","author":"ritik hansda","twitter_card":"summary_large_image","twitter_misc":{"Written by":"ritik hansda","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/#article","isPartOf":{"@id":"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/"},"author":{"name":"ritik hansda","@id":"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/c0119ddd2d27ea9cd476e809c8294ba7"},"headline":"PHP Variables","datePublished":"2024-03-02T16:51:22+00:00","dateModified":"2024-03-02T16:52:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/"},"wordCount":306,"commentCount":0,"articleSection":["What Is Variable And How To Declare Php Variable?"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.xopsschool.com\/tutorials\/php-variables\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/","url":"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/","name":"PHP Variables - XOps Tutorials!!!","isPartOf":{"@id":"https:\/\/www.xopsschool.com\/tutorials\/#website"},"datePublished":"2024-03-02T16:51:22+00:00","dateModified":"2024-03-02T16:52:17+00:00","author":{"@id":"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/c0119ddd2d27ea9cd476e809c8294ba7"},"breadcrumb":{"@id":"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.xopsschool.com\/tutorials\/php-variables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.xopsschool.com\/tutorials\/php-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.xopsschool.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"PHP Variables"}]},{"@type":"WebSite","@id":"https:\/\/www.xopsschool.com\/tutorials\/#website","url":"https:\/\/www.xopsschool.com\/tutorials\/","name":"XOps Tutorials!!!","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.xopsschool.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/c0119ddd2d27ea9cd476e809c8294ba7","name":"ritik hansda","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.xopsschool.com\/tutorials\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6fe0bc437410899c524975272622377e50e726108b1ad03f7b4488f734304fce?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6fe0bc437410899c524975272622377e50e726108b1ad03f7b4488f734304fce?s=96&d=mm&r=g","caption":"ritik hansda"},"url":"https:\/\/www.xopsschool.com\/tutorials\/author\/ritikhansda\/"}]}},"_links":{"self":[{"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":1,"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":223,"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/posts\/219\/revisions\/223"}],"wp:attachment":[{"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xopsschool.com\/tutorials\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}