{"id":40994,"date":"2021-02-10T20:23:39","date_gmt":"2021-02-10T20:23:39","guid":{"rendered":"https:\/\/www.oracletutoring.ca\/blog\/?p=40994"},"modified":"2021-02-10T20:23:41","modified_gmt":"2021-02-10T20:23:41","slug":"php-the-global-keyword","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/php-the-global-keyword\/","title":{"rendered":"Php: the <em>global<\/em> keyword"},"content":{"rendered":"\n<h2>Self-tutoring about PHP: the tutor mentions an idea about PHP <em>global<\/em>.<\/h2>\n<p>Consider the file<\/p>\n<p>$var1=&#8221;hello&#8221;;<\/p>\n<p>function f1(){<\/p>\n<p>echo $var1;<\/p>\n<p>}<\/p>\n<p>f1(); \/\/call f1<\/p>\n<p>In the code above, with almost any programming language I know of, $var1 has value &#8220;hello&#8221; in the function f1. f1 can override it, but if not, then $var1 maintains its external, &#8220;global&#8221; value: &#8220;hello&#8221;.<\/p>\n<p>With the version of PHP I use, however, the $var1 inside f1 is undefined. To make it take on the value of the external $var1, it needs to be declared in f1 as global:<\/p>\n<p>$var1=&#8221;hello&#8221;;<\/p>\n<p>function f1(){<\/p>\n<p>global $var1;<\/p>\n<p>echo $var1;<\/p>\n<p>}<\/p>\n<p>With the <em>global $var1<\/em> declaration, a call to f1 will cause it to output &#8220;hello&#8221;.<\/p>\n<p>Source:<\/p>\n<p><a href=\"https:\/\/www.php.net\/manual\/en\/language.variables.scope.php\">php.net<\/a><\/p>\nJack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.\n\n","protected":false},"excerpt":{"rendered":"<p>Self-tutoring about PHP: the tutor mentions an idea about PHP global. Consider the file $var1=&#8221;hello&#8221;; function f1(){ echo $var1; } f1(); \/\/call f1 In the code above, with almost any programming language I know of, $var1 has value &#8220;hello&#8221; in &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/php-the-global-keyword\/\"> <span class=\"screen-reader-text\">Php: the <em>global<\/em> keyword<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2955],"tags":[],"class_list":["post-40994","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/40994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=40994"}],"version-history":[{"count":11,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/40994\/revisions"}],"predecessor-version":[{"id":41005,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/40994\/revisions\/41005"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=40994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=40994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=40994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}