<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>最优良人 &#187; 变量</title>
	<atom:link href="http://www.zui88.com/view-tag/%e5%8f%98%e9%87%8f/feed" rel="self" type="application/rss+xml" />
	<link>http://www.zui88.com/blog</link>
	<description>中山php&#124;最优网络</description>
	<lastBuildDate>Mon, 13 May 2013 04:56:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>php函数体里调用外部变量</title>
		<link>http://www.zui88.com/blog/view-143.html</link>
		<comments>http://www.zui88.com/blog/view-143.html#comments</comments>
		<pubDate>Thu, 18 Aug 2011 13:47:21 +0000</pubDate>
		<dc:creator>lin</dc:creator>
				<category><![CDATA[后端程序]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[变量]]></category>

		<guid isPermaLink="false">http://www.zui88.com/blog/?p=143</guid>
		<description><![CDATA[在程序开发中，有时在一个函数里面需要调用到函数体以外的变量，这个时候有几种方法 可以再声明变量的时候声明为全局变量，如： global $string; $string = 'test'; function __(){ return $string; } 也可以在函数的内部声明，如： $string = 'test'; function __(){ global $string; return $string; } 当需要调用的变量只有少数的时候可以这样用，那么如果是需要使用大量已经定义过的变量或者甚至是全部变量的时候如何处理呢？可以这样处理，用到PHP的超全局数组$GLOBALS和extract()函数 PHP手册对$GLOBAL的说明是这样的： An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array. Note: This [...]]]></description>
			<content:encoded><![CDATA[<div id="blog_text" class="cnt">
<p>在程序开发中，有时在一个函数里面需要调用到函数体以外的变量，这个时候有几种方法</p>
<p>可以再声明变量的时候声明为全局变量，如：</p>
<p><span style="background-color: #d9d9d9;">global $string;</span></p>
<p><span style="background-color: #d9d9d9;">$string = 'test';</span></p>
<p><span style="background-color: #d9d9d9;">function __(){</span><br />
<span style="background-color: #d9d9d9;">     return $string;</span><br />
<span style="background-color: #d9d9d9;">}</span></p>
<p><span>也可以在函数的内部声明，如：</span></p>
<p><span style="background-color: #d9d9d9;"><span style="background-color: #d9d9d9;">$string = 'test';</span></p>
<p><span style="background-color: #d9d9d9;">function __(){</span></p>
<p><span style="background-color: #d9d9d9;">    global $string;</span><br />
<span style="background-color: #d9d9d9;">     return $string;</span><br />
<span style="background-color: #d9d9d9;">}</span></p>
<p><span style="background-color: #ffffff;">当需要调用的变量只有少数的时候可以这样用，那么如果是需要使用大量已经定义过的变量或者甚至是全部变量的时候如何处理呢？可以这样处理，用到PHP的超全局数组$GLOBALS和extract()函数</span></p>
<p><span style="background-color: #ffffff;">PHP手册对$GLOBAL的说明是这样的：</span></p>
<p><span style="background-color: #ffffff;">An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.</p>
<p><strong>Note</strong>: This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do <strong>global $variable;</strong> to access it within functions or methods.</p>
<p>大概意思是：</p>
<p>这个一个由所有变量组成的数组。变量名就是该数组的索引。并且该数组是超全局数组，在使用时不必声明<strong>global $variable;</strong></p>
<p>extract()函数的作用是把数组的键名作为变量名，数组的键值作为变量的值。</p>
<p>所以综上所述，只要在函数体里面写上下面一句话就可以实现调用到外部的所有变量了</p>
<p><span style="background-color: #d9d9d9;"> </span></p>
<p><span style="background-color: #d9d9d9;">$string = 'test';</span></p>
<p><span style="background-color: #d9d9d9;">$num = 100;</span></p>
<p><span style="background-color: #d9d9d9;">function __(){</span></p>
<p><span style="background-color: #d9d9d9;"></span><span style="background-color: #d9d9d9;">     echo$string,$num;</span><br />
<span style="background-color: #d9d9d9;">}</span></p>
<p>    extract($GLOBALS,EXTR_SKIP);</p>
<p></span></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.zui88.com/blog/view-143.html/feed</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>
