{"id":129,"date":"2007-09-15T09:06:08","date_gmt":"2007-09-15T01:06:08","guid":{"rendered":"http:\/\/learn-house.idv.tw\/?p=129"},"modified":"2010-01-17T19:16:30","modified_gmt":"2010-01-17T11:16:30","slug":"php%e5%85%a8%e5%9f%9f%e8%ae%8a%e6%95%b8-global","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=129","title":{"rendered":"[PHP]\u5168\u57df\u8b8a\u6578-global"},"content":{"rendered":"<p><strong>\u5167\u5bb9\u4f86\u6e90\uff1a<a href=\"http:\/\/linux.tnc.edu.tw\/techdoc\/php\/php_big5\/language.variables.scope.html\" target=\"_blank\">Link<\/a>&nbsp;&nbsp;&nbsp; <\/strong><strong>(\u6b64\u7bc7\u6587\u7ae0\u50c5\u4f9b\u7ad9\u9577\u5099\u4efd\u53c3\u8003\u7528\uff0c\u56fa\u70ba\u52a0\u5bc6\u6587\u7ae0)<\/strong><\/p>\n<p>\u8b8a\u6578\u7684\u7bc4\u570d\u5373\u5b83\u5b9a\u7fa9\u7684\u4e0a\u4e0b\u6587\u80cc\u666f\uff08\u8b6f\u8005:\u8aaa\u767d\u4e86\uff0c\u4e5f\u5c31\u662f\u5b83\u7684\u751f\u6548\u7bc4\u570d\uff09\u3002\u5927\u90e8\u5206\u7684 PHP \u8b8a\u6578\u53ea\u6709\u4e00\u500b\u55ae\u7368\u7684\u7bc4\u570d\u3002\u9019\u500b\u55ae\u7368\u7684\u7bc4\u570d\u8de8\u5ea6\u540c\u6a23\u5305\u542b\u4e86 include \u548c require \u5f15\u5165\u7684\u6587\u4ef6\u3002\u7bc4\u4f8b: <\/p>\n<div class=\"informalexample\"><a name=\"AEN4244\" title=\"AEN4244\"><\/a><\/p>\n<table bgcolor=\"#e0e0e0\" border=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>\n<pre class=\"php\">&lt;?php<br \/>$a = 1;<br \/>include &quot;b.inc&quot;;<br \/>?&gt;<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u9019\u88e1\u8b8a\u6578 <tt class=\"varname\">$a<\/tt> \u5c07\u6703\u5728\u5305\u542b\u6587\u4ef6 <tt class=\"filename\">b.inc<\/tt> \u4e2d\u751f\u6548\u3002\u4f46\u662f\uff0c\u5728\u7528\u6236\u81ea\u5b9a\u7fa9\u51fd\u5f0f\u4e2d\uff0c\u4e00\u500b\u5c40\u90e8\u51fd\u5f0f\u7bc4\u570d\u5c07\u88ab\u5f15\u5165\u3002\u4efb\u4f55\u7528\u65bc\u51fd\u5f0f\u5167\u90e8\u7684\u8b8a\u6578\u6309\u9810\u8a2d\u60c5\u6cc1\u5c07\u88ab\u9650\u5236\u5728\u5c40\u90e8\u51fd\u5f0f\u7bc4\u570d\u5167\u3002\u7bc4\u4f8b\uff1a <\/p>\n<div class=\"informalexample\"><a name=\"AEN4249\" title=\"AEN4249\"><\/a><\/p>\n<table bgcolor=\"#e0e0e0\" border=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>\n<pre class=\"php\">&lt;?php<br \/>$a = 1; \/* global scope *\/ <br \/><br \/>function Test()<br \/>{ <br \/>    echo $a; \/* reference to local scope variable *\/ <br \/>} <br \/><br \/>Test();<br \/>?&gt;<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u9019\u500b\u8173\u672c\u4e0d\u6703\u6709\u4efb\u4f55\u8f38\u51fa\u56e0\u70ba echo \u8a9e\u53e5\u5f15\u7528\u4e86\u4e00\u500b\u5c40\u90e8\u7248\u672c\u7684\u8b8a\u6578 <tt class=\"varname\">$a<\/tt>\uff0c \u800c\u4e14\u5728\u9019\u500b\u7bc4\u570d\u5167\uff0c\u5b83\u4e26\u6c92\u6709\u88ab\u8a2d\u503c\u3002\u4f60\u53ef\u80fd\u6ce8\u610f\u5230 PHP \u7684\u5168\u57df\u8b8a\u6578\u548c C \u8a9e\u8a00\u6709\u4e00\u9ede\u9ede\u4e0d\u540c\uff0c\u5728 C \u8a9e\u8a00\u4e2d\uff0c\u5168\u57df\u8b8a\u6578\u5728\u51fd\u5f0f\u4e2d\u81ea\u52d5\u751f\u6548\uff0c\u9664\u975e\u88ab\u5c40\u90e8\u8b8a\u6578\u8986\u84cb\u3002\u9019\u53ef\u80fd\u5f15\u8d77\u4e00\u4e9b\u554f\u984c\uff0c\u6709\u4e9b\u4eba\u53ef\u80fd\u6f2b\u4e0d\u7d93\u5fc3\u7684\u6539\u8b8a\u4e00\u500b\u5168\u57df\u8b8a\u6578\u3002 PHP \u4e2d\u5168\u57df\u8b8a\u6578\u5728\u51fd\u5f0f\u4e2d\u4f7f\u7528\u6642\u5fc5\u9808\u7533\u660e\u70ba\u5168\u57df\u3002\u4f8b\u5982\uff1a <\/p>\n<div class=\"informalexample\"><a name=\"AEN4253\" title=\"AEN4253\"><\/a><\/p>\n<table bgcolor=\"#e0e0e0\" border=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>\n<pre class=\"php\">&lt;?php<br \/>$a = 1;<br \/>$b = 2;<br \/><br \/>function Sum()<br \/>{<br \/>    global $a, $b;<br \/><br \/>    $b = $a + $b;<br \/>} <br \/><br \/>Sum();<br \/>echo $b;<br \/>?&gt;<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u4ee5\u4e0a\u8173\u672c\u7684\u8f38\u51fa\u5c07\u662f &quot;3&quot;\u3002\u5728\u51fd\u5f0f\u4e2d\u7533\u660e\u4e86\u5168\u57df\u8b8a\u6578 <tt class=\"varname\">$a<\/tt> \u548c <tt class=\"varname\">$b<\/tt>\uff0c\u4efb\u4f55\u8b8a\u6578\u7684\u6240\u6709\u5f15\u7528\u8b8a\u6578\u90fd\u6703\u6307\u5411\u5230\u5168\u57df\u8b8a\u6578\u3002\u5c0d\u65bc\u4e00\u500b\u51fd\u5f0f\u80fd\u5920\u7533\u660e\u7684\u5168\u57df\u8b8a\u6578\u7684\u6700\u5927\u500b\u6578\uff0cPHP \u6c92\u6709\u9650\u5236\u3002 <\/p>\n<p>\u5728\u5168\u57df\u7bc4\u570d\u5167\u9023\u63a5\u8b8a\u6578\u7684\u7b2c\u4e8c\u500b\u8fa6\u6cd5\uff0c\u662f\u7528\u7279\u6b8a\u7684 PHP \u81ea\u5b9a\u7fa9 <tt class=\"varname\">$GLOBALS<\/tt> \u9663\u5217\u3002\u524d\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u5beb\u6210\uff1a <\/p>\n<div class=\"informalexample\"><a name=\"AEN4260\" title=\"AEN4260\"><\/a><\/p>\n<table bgcolor=\"#e0e0e0\" border=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>\n<pre class=\"php\">&lt;?php<br \/>$a = 1;<br \/>$b = 2;<br \/><br \/>function Sum()<br \/>{<br \/>    $GLOBALS[&quot;b&quot;] = $GLOBALS[&quot;a&quot;] + $GLOBALS[&quot;b&quot;];<br \/>} <br \/><br \/>Sum();<br \/>echo $b;<br \/>?&gt;<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u5728 <tt class=\"varname\">$GLOBALS<\/tt> \u9663\u5217\u4e2d\uff0c\u6bcf\u4e00\u500b\u8b8a\u6578\u70ba\u4e00\u500b\u5143\u7d20\uff0c\u9375\u540d\u5c0d\u61c9\u8b8a\u6578\u540d\uff0c\u503c\u8b8a\u6578\u7684\u5167\u5bb9\u3002<tt class=\"varname\">$GLOBALS<\/tt> \u4e4b\u6240\u4ee5\u5728\u5168\u57df\u7bc4\u570d\u5167\u5b58\u5728\uff0c\u662f\u56e0\u70ba $GLOBALS \u662f\u4e00\u500b <a href=\"http:\/\/linux.tnc.edu.tw\/techdoc\/php\/php_big5\/language.variables.predefined.html#language.variables.superglobals\">superglobal<\/a> \u8b8a\u6578\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5167\u5bb9\u4f86\u6e90\uff1aLink&nbsp;&nbsp;&nbsp; (\u6b64\u7bc7\u6587\u7ae0\u50c5\u4f9b\u7ad9\u9577\u5099\u4efd\u53c3\u8003\u7528\uff0c\u56fa\u70ba\u52a0\u5bc6\u6587\u7ae0) \u8b8a\u6578\u7684\u7bc4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/129"}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=129"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}