<?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; zval</title>
	<atom:link href="http://www.laruence.com/tag/zval/feed" rel="self" type="application/rss+xml" />
	<link>http://www.laruence.com</link>
	<description>PHP语言, PHP扩展, Zend引擎相关的研究,技术,新闻分享 - 左手代码 右手诗</description>
	<lastBuildDate>Wed, 08 Feb 2012 05:12:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>深入理解PHP原理之变量(Variables inside PHP)</title>
		<link>http://www.laruence.com/2008/08/22/412.html</link>
		<comments>http://www.laruence.com/2008/08/22/412.html#comments</comments>
		<pubDate>Fri, 22 Aug 2008 14:36:18 +0000</pubDate>
		<dc:creator>雪候鸟</dc:creator>
				<category><![CDATA[PHP应用]]></category>
		<category><![CDATA[PHP源码分析]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[variable]]></category>
		<category><![CDATA[Zend/PHP]]></category>
		<category><![CDATA[zval]]></category>

		<guid isPermaLink="false">http://www.laruence.com/?p=412</guid>
		<description><![CDATA[<p>或许你知道，或许你不知道，PHP是一个弱类型，动态的脚本语言。所谓弱类型，就是说PHP并不严格验证变量类型(严格来讲，PHP是一个中强类型语言,这部分内容会在以后的文章中叙述)，在申明一个变量的时候，并不需要显示指明它保存的数据的类型。而PHP的核心ZE是用C编写的，大家都知道C是一个强类型语言，也就是说，在C中所有的变量在它被声明到最终销毁，都只能保存一种类型的数据。 那么PHP是如何在ZE的基础上实现弱类型的呢？且听我慢慢道来....</p>]]></description>
			<content:encoded><![CDATA[<div class="copyright" >
<ul  style="padding-left:1em;font-size:85%;padding-left:1em;font-size:85%;">
<li>作者: <a href="http://www.laruence.com" >Laruence</a>(<a href="http://www.twitter.com/laruence"  target="meme"  title="Twitter" ><img src="/images/ico-twitter.png" /></a> <a href="http://t.sina.com/laruence"  target="meme"  title="新浪微博" ><img src="/images/ico-sina.png" /></a> <a href="http://fusion.google.com/add?feedurl=http://www.laruence.com/feed"  target="meme"  title="Google阅读器" ><img src="/images/ico-google.png" /></a> <a href="mailto:laruence@yahoo.com.cn"  target="meme"  title="邮件" ><img src="/images/ico-mail.png" /></a>)</li>
<li>本文地址: <a href="http://www.laruence.com/2008/08/22/412.html"  title="Permanet Link to 深入理解PHP原理之变量(Variables inside PHP)" >http://www.laruence.com/2008/08/22/412.html</a></li>
</li>
<li>转载请注明出处 </li>
</ul></div>
<p>或许你知道，或许你不知道，PHP是一个弱类型，动态的脚本语言。所谓弱类型，就是说PHP并不严格验证变量类型(严格来讲，PHP是一个中强类型语言,这部分内容会在以后的文章中叙述)，在申明一个变量的时候，并不需要显示指明它保存的数据的类型：</p>
<pre name="code"  class="sh_php"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
&lt;?php
  $var = 1; //int
  $var = &quot;laruence&quot;; //string
  $var = 1.0002; //float
  $var = array(); // array
  $var = new Exception('error'); //object;
</pre>
<p>  动态语言，就是说，PHP的语言结构在运行期是可以改变的，比如我们在运行期require一个函数定义文件，从而导致语言的函数表动态的改变。<br/>
  所谓脚本语言，就是说，PHP并不是独立运行的，要运行PHP我们需要PHP解析器：</p>
<pre name="code"  class="sh_shell"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
  /usr/bin/php -f example.php
</pre>
<p>   我前面的文章中已经讲过，PHP的执行是通过Zend engine(ZE, Zend引擎), ZE是用C编写的，大家都知道C是一个强类型语言，也就是说，在C中所有的变量在它被声明到最终销毁，都只能保存一种类型的数据。 那么PHP是如何在ZE的基础上实现弱类型的呢？</p>
<div style="color:blue;padding-lef" >首先要声明一点，如果你以前没有接触过PHP的源码分析，扩展开发。 如果你并不了解PHP的架构， 没有听说ZE，那么我建议你先看看我前面的文章,尤其推荐：</p>
<li><a href="http://www.laruence.com/2008/08/11/147.html" >深入浅出PHP(PHP Internals)</a></li>
<li><a href="http://www.laruence.com/2008/06/18/221.html" >深入理解PHP原理之Opcodes</a></li>
</div>
<p>   在PHP中，所有的变量都是用一个结构-zval来保存的， 在Zend/zend.h中我们可以看到zval的定义：</p>
<pre name="code"  class="sh_cpp"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
  typedef struct _zval_struct {
    zvalue_value value;
    zend_uint refcount;
    zend_uchar type;
    zend_uchar is_ref;
  } zval;
 </pre>
<p>    其中zvalue_value是真正保存数据的关键部分，现在到了揭晓谜底的时候了，PHP是如何在ZE的基础上实现弱类型的呢？ 因为zvalue_value是个联合体(union), </p>
<pre name="code"  class="sh_cpp"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
typedef union _zvalue_value {
    long lval;
    double dval;
    struct {
        char *val;
        int len;
    } str;
    HashTable *ht;
    zend_object_value obj;
} zvalue_value;
</pre>
<p>   那么这个结构是如何储存PHP中的多种类型的呢？<br/>
   PHP中常见的变量类型有：</p>
<pre name="code"  class="sh_shell"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
1. 整型/浮点/长整型/bool值 等等
2. 字符串
3. 数组/关联数组
4. 对象
5. 资源
  </pre>
<p>   PHP根据zval中的type字段来储存一个变量的真正类型，然后根据type来选择如何获取zvalue_value的值，比如对于整型和bool值:</p>
<pre name="code"  class="sh_shell"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
   zval.type = IS_LONG;//整形
   zval.type = IS_BOOL;//布尔值
</pre>
<p>   就去取zval.value.lval,对于bool值来说lval∈(0|1);<br/>
   如果是双精度，或者float则会去取zval.value的dval。<br/>
   而如果是字符串，那么:</p>
<pre name="code"  class="sh_shell"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
   zval.type = IS_STRING
</pre>
<p>   这个时候，就会取:<br/>
    zval.value.str<br/>
   而这个也是个结构，存有C分格的字符串和字符串的长度。</p>
<p>   而对于数组和对象，则type分别对应IS_ARRAY, IS_OBJECT, 相对应的则分别取zval.value.ht和obj</p>
<p>   比较特别的是资源，在PHP中，资源是个很特别的变量，任何不属于PHP内建的变量类型的变量，都会被看作成资源来进行保存，比如，数据库句柄，打开的文件句柄等等。 对于资源:</p>
<pre name="code"  class="sh_php"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
   type = IS_RESOURCE
</pre>
<p>   这个时候，会去取zval.value.lval， 此时的lval是个整型的指示器， 然后PHP会再根据这个指示器在PHP内建的一个资源列表中查询相对应的资源(这部分的内容，我以后会单独开一个篇文章来介绍)，目前，你只要知道此时的lval就好像是对应于资源链表的偏移值。</p>
<pre name="code"  class="sh_php"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
 ZEND_FETCH_RESOURCE(con, type, zval *, default, resource_name, resource_type);
</pre>
<p>    借用这样的机制，PHP就实现了弱类型，因为对于ZE的来说，它所面对的永远都是同一种类型，那就是zval。<br/>
    ps:明天team出去building，我想着应该在走之前写点东西给我的blog reader来消磨周末。今天就简单先开个头，下一次，我将进一步介绍PHP的变量，作用域，以及变量的copy on write和change on write机制， 待续&#8230;.<br/>
  <script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_php.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_shell.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_cpp.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_cpp.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_shell.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_shell.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_shell.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_php.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_php.js" ></script></p>
<hr/><h2>Comments</h2><ul  style="padding-left:1em;font-size:85%;padding-left:1em;font-size:85%;"><li><a href="http://www.laruence.com/2008/08/22/412.html" >2008/08/23</a>, 左手 writes: 太佩服你了，强大的很</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2008/08/26</a>, <a href="http://fy"  rel="external nofollow"  class="url" >fj</a> writes: mark</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2008/08/27</a>, bush writes: 在PHP中，所有的变量都是用一个结构-zval来保存的， 在Zend/zend.h中我们可以看到zval的定义：

在php应用中 Zend相关的文件没有找到或见过  它属于哪一部分呢</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2008/08/27</a>, <a href="http://www.laruence.com"  rel="external nofollow"  class="url" >雪候鸟</a> writes: 我指的Zend/zend.h是相当于PHP源码结构树的根目录
比如,我看的是PHP5.2,那么Zend就是
PHP5.2-SRC/Zend/</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2008/09/23</a>, 狂笨地太阳 writes: 太好了，终于理解PHP的变量是怎么个样子了。。PHP变量类型不同，ZVAL的TYPE就不同。</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2008/11/25</a>, <a href="http://www.benleo.cn/?p=186"  rel="external nofollow"  class="url" >完全COPY : 深入理解PHP原理之变量分离/引用(Variables Separation)</a> writes: [...] Separation) 在前面的文章中我已经介绍了PHP的变量的内部表示(深入理解PHP原理之变量(Variables inside PHP))，以及PHP中作用域的实现机制(深入理解PHP原理之变量作用域(Scope inside [...]</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2008/11/26</a>, yzcj007 writes: 感谢搂主，写得很棒，加油写，等着看呢！</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2010/03/25</a>, Anonymous writes: 牛人</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2010/05/18</a>, <a href="http://www.laruence.com/2010/05/18/1482.html"  rel="external nofollow"  class="url" >深入理解PHP原理之对象(一) | 风雪之隅</a> writes: [...] 对象的结构 在PHP5中, 一个对象, 还是以一个zval做为载体的, 还记得什么是Zval么(深入理解PHP原理之变量). [...]</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2010/05/26</a>, <a href="http://crazyphper.com/wblog/archives/777"  rel="external nofollow"  class="url" >深入理解 PHP之require/include顺序 &laquo; 大熊猫 &#8211; konakona ——PHP程序员</a> writes: [...] 在PHP5中, 一个对象, 还是以一个zval做为载体的, 还记得什么是Zval么(深入理解 PHP原理之变量). [...]</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2010/07/30</a>, <a href="http://zhidao.123doing.com/32792.html"  rel="external nofollow"  class="url" >关于引用的一个小问题。。。散分。。 - PHP常见问题 - 关于引用 一个小问题 散分 PHP 基础编程 - 123Doing</a> writes: [...] unset只是将$t指向的zval结构体的refcount &#8211; 1,然后清除符号表里的&#8217;t&#39;,去除$t与zval的关联但是$s仍然是关联zval的。我说不明白，有一篇高手写的文章专门讲这个的，在本坛我贴出来不少次了，你认真看看。http://www.laruence.com/2008/08/22/412.html [...]</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2010/09/19</a>, <a href="http://www.yakecan.com/archives/460"  rel="external nofollow"  class="url" >(转)深入理解PHP原理之变量分离/引用(Variables Separation) &raquo; Creative Power</a> writes: [...] 在前面的文章中我已经介绍了PHP的变量的内部表示(深入理解PHP原理之变量(Variables inside PHP))，以及PHP中作用域的实现机制(深入理解PHP原理之变量作用域(Scope inside PHP))。这节我们就接着前面的文章，继续介绍PHP中变量分离和引用的概念： [...]</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2011/01/31</a>, <a href="http://zhidao.123doing.com/52141.html"  rel="external nofollow"  class="url" >关于引用的一个小问题。。。散分。。 - PHP常见问题 - [标签:tags] - 开源网 | 123Doing</a> writes: [...] 如果你在php4和php5中分别执行上述代码，则php4 得到 21php5 得到 22  unset只是将$t指向的zval结构体的refcount &#8211; 1,然后清除符号表里的&#8217;t&#039;,去除$t与zval的关联但是$s仍然是关联zval的。我说不明白，有一篇高手写的文章专门讲这个的，在本坛我贴出来不少次了，你认真看看。http://www.laruence.com/2008/08/22/412.html 关键是这篇http://www.laruence.com/2008/09/19/520.html然后再结合php的GC原理会更深入一些。http://www.php.net/manual/en/features.gc.refcounting-basics.php [...]</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2011/03/18</a>, <a href="http://51nosql.com"  rel="external nofollow"  class="url" >51nosql</a> writes: 向大牛学习，关注</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2011/03/20</a>, <a href="http://www.iirr.info/blog/?p=155"  rel="external nofollow"  class="url" >HorseLuke@微碌 &raquo; Blog Archive &raquo; php缓存扩展频繁存储/读取数组引发CPU过高问题排查手记（php-memcache为例）</a> writes: [...] [2]laruence. 深入理解PHP原理之变量(Variables inside PHP)：http://www.laruence.com/2008/08/22/412.html [...]</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2011/03/23</a>, liano writes: 牛人啊，我现在在学php能否给我个联系地址请教你一下啊？
qq：584418561</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2011/08/23</a>, Forever writes: 照这样说每个变量占用的内存都比较大，因为它是结构体类型啊。结构体占用的内存不是所有类型加起来的嘛</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2011/08/23</a>, <a href="http://www.laruence.com"  rel="external nofollow"  class="url" >雪候鸟</a> writes: @Forever 不是结构体, 是union ;)</li><li><a href="http://www.laruence.com/2008/08/22/412.html" >2011/09/02</a>, <a href="http://warpigallen.sinaapp.com/?p=13"  rel="external nofollow"  class="url" >深入理解PHP原理之变量(Variables inside PHP) | warpig_allen&#039;s blog</a> writes: [...] 本文地址: http://www.laruence.com/2008/08/22/412.html [...]</li></ul><hr/><h2>Related posts:</h2><ul  style="padding-left:1em;font-size:85%;padding-left:1em;font-size:85%;"><li><a href="http://www.laruence.com/2008/08/26/463.html"  rel="bookmark"  title="Permanent Link: 深入理解PHP原理之变量作用域(Scope in PHP)" >深入理解PHP原理之变量作用域(Scope in PHP)</a></li><li><a href="http://www.laruence.com/2008/09/19/520.html"  rel="bookmark"  title="Permanent Link: 深入理解PHP原理之变量分离/引用(Variables Separation)" >深入理解PHP原理之变量分离/引用(Variables Separation)</a></li><li><a href="http://www.laruence.com/2009/06/01/905.html"  rel="bookmark"  title="Permanent Link: 开心网偷菜外挂" >开心网偷菜外挂</a></li><li><a href="http://www.laruence.com/2009/07/27/1020.html"  rel="bookmark"  title="Permanent Link: 深入理解PHP原理之错误抑制与内嵌HTML" >深入理解PHP原理之错误抑制与内嵌HTML</a></li><li><a href="http://www.laruence.com/2011/03/29/1949.html"  rel="bookmark"  title="Permanent Link: 深入理解PHP原理之Session Gc的一个小概率Notice" >深入理解PHP原理之Session Gc的一个小概率Notice</a></li></ul><hr/><small  style="font-size:85%;font-size:85%;">Copyright &copy; 2010 <a href="http://www.laruence.com"  target="_blank" >风雪之隅</a> 版权所有, 转载务必注明. 该Feed只供个人使用, 禁止未注明的转载或商业应用. 非法应用的, 一切法律后果自负. 如有问题, 可发E-mail至my at laruence.com.(Digital Fingerprint: 73540ba0a1738d7d07d4b6038d5615e2)</small><h2 class="related_post_title" >Related Posts:</h2><ul class="related_post"   style="padding-left:1em;font-size:85%;padding-left:1em;font-size:85%;"><li><a href="http://www.laruence.com/2009/12/26/1198.html"  title="深入理解PHP原理之变量生命期(一)" >深入理解PHP原理之变量生命期(一)</a></li><li><a href="http://www.laruence.com/2008/09/23/539.html"  title="使用PHP Embed SAPI实现Opcodes查看器" >使用PHP Embed SAPI实现Opcodes查看器</a></li><li><a href="http://www.laruence.com/2008/09/19/520.html"  title="深入理解PHP原理之变量分离/引用(Variables Separation)" >深入理解PHP原理之变量分离/引用(Variables Separation)</a></li><li><a href="http://www.laruence.com/2008/08/26/463.html"  title="深入理解PHP原理之变量作用域(Scope in PHP)" >深入理解PHP原理之变量作用域(Scope in PHP)</a></li><li><a href="http://www.laruence.com/2008/08/15/274.html"  title="PHP 源代码分析 V0.0.2" >PHP 源代码分析 V0.0.2</a></li><li><a href="http://www.laruence.com/2008/08/14/250.html"  title="实现PHP的编译执行分离(separating compilation and execution)" >实现PHP的编译执行分离(separating compilation and execution)</a></li><li><a href="http://www.laruence.com/2008/08/12/180.html"  title="深入理解Zend SAPIs(Zend SAPI Internals)" >深入理解Zend SAPIs(Zend SAPI Internals)</a></li><li><a href="http://www.laruence.com/2008/08/12/164.html"  title="深入理解PHP原理之函数(Introspecting PHP Function)" >深入理解PHP原理之函数(Introspecting PHP Function)</a></li><li><a href="http://www.laruence.com/2008/08/11/147.html"  title="深入浅出PHP(Exploring PHP)" >深入浅出PHP(Exploring PHP)</a></li><li><a href="http://www.laruence.com/2008/06/18/221.html"  title="深入理解PHP原理之Opcodes" >深入理解PHP原理之Opcodes</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.laruence.com/2008/08/22/412.html/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

