{"id":275,"date":"2018-01-04T11:34:45","date_gmt":"2018-01-04T11:34:45","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=275"},"modified":"2018-01-04T11:34:45","modified_gmt":"2018-01-04T11:34:45","slug":"use-global-variable","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/use-global-variable\/","title":{"rendered":"Use Global Variable"},"content":{"rendered":"<p>The Global variable comes with a special characteristic. You can use them anywhere on the page having the php tag.<\/p>\n<p>Here is the example.<\/p>\n<p>[php]&lt;?php<br \/>\n$x = 5; \/\/ global scope<br \/>\nfunction testbar() {<br \/>\n    echo &quot;&lt;p&gt;Variable x inside function is: $x&lt;\/p&gt;&quot;;<br \/>\n}<br \/>\ntestbar();<br \/>\necho &quot;&lt;p&gt;Variable x outside function is: $x&lt;\/p&gt;&quot;;<br \/>\n?&gt;[\/php]<\/p>\n<p>The above code has Global variable &#8220;<code>$x<\/code>&#8221; which has given value &#8220;5&#8221;. If I run the code I will get a result.<\/p>\n<p>[php]Variable x inside function is:<\/p>\n<p>Variable x outside function is: 5[\/php]<\/p>\n<p>You can see the php code ignored the first sentence and provided nothing to X value. On other hands, the second sentence got the value from the Global variable as it is mentioned after the curly bracket of the function code.<\/p>\n<p>You can call this variable from anywhere without having separate function tag. You just need to put the basic php tag to get the global variable anywhere on the page.<\/p>\n<p>for e.g.<\/p>\n<p>[php]&lt;?php<br \/>\necho &quot;&lt;p&gt;Variable x outside function is: $x&lt;\/p&gt;&quot;;<br \/>\n?&gt;[\/php]<\/p>\n<p>The code can get the value of the &#8220;<code>$x<\/code>&#8221; from the Global variable. I can put this code anywhere on the same page where the global variable is available to get the value of X. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Global variable comes with a special characteristic. You can use them anywhere on the page having the php tag. Here is the example. [php]&lt;?php<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-275","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/comments?post=275"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/275\/revisions"}],"predecessor-version":[{"id":276,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/275\/revisions\/276"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}