<?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; PHP帮助手册</title>
	<atom:link href="https://www.phppan.com/tag/php%e5%b8%ae%e5%8a%a9%e6%89%8b%e5%86%8c/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sat, 25 Apr 2026 00:56:17 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>PHP手册拾遗四：变量函数</title>
		<link>https://www.phppan.com/2010/10/php-manual-4-variable/</link>
		<comments>https://www.phppan.com/2010/10/php-manual-4-variable/#comments</comments>
		<pubDate>Mon, 25 Oct 2010 09:28:10 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP帮助手册]]></category>
		<category><![CDATA[变量函数]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1069</guid>
		<description><![CDATA[PHP手册拾遗：变量函数 1、isset()函数 若使用 isset() 测试一个被设置成 NULL 的变量， [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>PHP手册拾遗：变量函数<br />
<strong>1、isset()函数</strong><br />
若使用 isset() 测试一个被设置成 NULL 的变量，将返回 FALSE。<br />
对于一个数组的元素，如果该元素的值为NULL，使用isset()函数将返回FALSE,此时需要使用array_key_exists函数。如下所示代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'t1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'t1'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>                                                               
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Yes'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'No'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'t1'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Yes'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'No'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>2、empty()函数</strong><br />
这是一个语言结构而非函数<br />
empty() 只检测变量，检测任何非变量的东西都将导致解析错误。换句话说，后边的语句将不会起作用： empty(addslashes($name))。<br />
如下示例：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;phppan&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//Parse error: syntax error, unexpected T_LNUMBER</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//  Fatal error: Can't use function return value in write context</span></pre></td></tr></table></div>

<p><strong>3、floatval()函数</strong><br />
float floatval ( mixed var )<br />
var 可以是任何标量类型。你不能将 floatval() 用于数组或对象。</p>
<p><strong>4、import_request_variables函数</strong><br />
将 GET／POST／Cookie 变量导入到全局作用域中。如果你禁止了 register_globals，但又想用到一些全局变量，那么此函数就很有用。<br />
很少用到此函数，一般都是直接使用$_GET／$_POST／$_COOKIE<br />
如果要将其它变量导入到全局变量中，可以考虑使用extract()。</p>
<p><strong>5、serialize()与unserialize() </strong><br />
<strong>serialize会存储数组／对象中的引用。因此可以通过序列化和反序列化实现深拷贝。</strong><br />
当序列化对象时，PHP 将试图在序列动作之前调用该对象的成员函数 __sleep()。这样就允许对象在被序列化之前做任何清除操作。类似的，当使用 unserialize() 恢复对象时， 将调用 __wakeup() 成员函数。</p>
<p><strong>6、unset &#8212; 释放给定的变量</strong><br />
如果在函数中 unset() 一个全局变量，则只是局部变量被销毁，而在调用环境中的变量将保持调用 unset() 之前一样的值。<br />
如下所示代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> unset_global_var<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
unset_global_var<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>如果在函数中 unset() 一个通过引用传递的变量，则只是局部变量被销毁，而在调用环境中的变量将保持调用 unset() 之前一样的值。<br />
如果在函数中 unset() 一个静态变量，则 unset() 将销毁此变量及其所有的引用。<br />
新版本的手册中的示例很能说明上面的这个问题：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$bar</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$bar</span><span style="color: #339933;">++;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Before unset: <span style="color: #006699; font-weight: bold;">$bar</span>, &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">23</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;after unset: <span style="color: #006699; font-weight: bold;">$bar</span>&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>输出：<br />
Before unset: 1, after unset: 23<br />
Before unset: 2, after unset: 23<br />
Before unset: 3, after unset: 23</p>
<p>如果在函数中 unset() 一个全局变量，可使用 $GLOBALS 数组来实现：</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/10/php-manual-4-variable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP帮助手册拾遗三：类与对象</title>
		<link>https://www.phppan.com/2010/09/php-manual-3-class-and-object/</link>
		<comments>https://www.phppan.com/2010/09/php-manual-3-class-and-object/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 06:27:49 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP帮助手册]]></category>
		<category><![CDATA[面向对象]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=980</guid>
		<description><![CDATA[PHP帮助手册拾遗：类与对象 1、__autoload函数 __autoload函数定义时必须包含一个参数，否 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>PHP帮助手册拾遗：类与对象</p>
<p><strong>1、__autoload函数</strong><br />
__autoload函数定义时必须包含一个参数，否则会显示如下错误：<br />
Fatal error: __autoload() must take exactly 1 argument</p>
<p>在 __autoload 函数中抛出的异常不能被 catch 语句块捕获并导致致命错误。</p>
<p><strong>2、父类的构造方法调用 </strong><br />
如果子类中定义了构造函数则不会暗中调用其父类的构造函数。要执行父类的构造函数，需要在子类的构造函数中调用 parent::__construct()。这里python和php一样</p>
<p><strong>3、父类的析构方法调用 </strong><br />
和构造函数一样，父类的析构函数不会被引擎暗中调用。要执行父类的析构函数，必须在子类的析构函数体中显式调用 parent::__destruct()。<br />
<strong>析构函数在脚本关闭时调用，此时所有的头信息已经发出。</strong><br />
试图在析构函数中抛出一个异常会导致致命错误。</p>
<p><strong>4、访问限制 </strong><br />
var与public在类中的变量定义相同，但是在php5的php5.1.3会生成一个E_STRICT警告<br />
方法如果没有设置访问控制，则将默认设置为public</p>
<p><strong>5、构造方法</strong><br />
PHP提交两种构造方法，以类名和__construct，当类中没有__construct方法时，PHP会调用类名函数，但是如果存在__construct时，不管其访问控制是什么，都不会调用类名函数。<br />
如下所示代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Demo <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Demo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Demo function'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'__construct function'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$demo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Demo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>运行会报错：Fatal error: Call to private Demo::__construct() from invalid context<br />
ps:上面的这个问题是ben前辈提出的，感谢ben前辈昨天的指导</p>
<p><strong>6、静态变量</strong><br />
PHP5.3以后，可以使用变量引用类，但是这个变量不能是关键字（如self,parent,static等）。如下所示代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Demo <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000088;">$my_static</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Demo'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$classname</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Demo'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$classname</span><span style="color: #339933;">::</span><span style="color: #000088;">$my_static</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// As of PHP 5.3.0</span></pre></td></tr></table></div>

<p><strong>7、接口</strong><br />
接口可以有常量，不能有变量，并且常量在其子类中无法重载<br />
如下所示代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> Base <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">const</span> <span style="color: #990000;">constant</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'constant value'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test var&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Foo implements Base <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> Base<span style="color: #339933;">::</span><span style="color: #990000;">constant</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> Base<span style="color: #339933;">::</span><span style="color: #000088;">$var</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>以上代码报错为：Fatal error: Interfaces may not include member variables<br />
注释掉对变量的操作后可以正常打印常量中存放的值。<br />
<strong>8、对象迭代</strong><br />
在PHP5中，使用foreach，遍历一个对象，可以访问这个对象的所有可以访问的属性<br />
如果要自定义迭代器，可以通过实现一个叫Iterator的接口来实现<br />
<strong>9、单例模式</strong><br />
<a href="http://www.phppan.com/2010/06/php-design-pattern-6-singleton/">单例模式(singleton模式)</a>在实现过程中，需要考虑__clone方法。<br />
<strong>10、对象与引用 </strong><br />
PHP5之后，对象在默认情况下是以引用的方式传递的。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/09/php-manual-3-class-and-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP帮助手册拾遗二：流程控制</title>
		<link>https://www.phppan.com/2010/08/php-manual-2-control/</link>
		<comments>https://www.phppan.com/2010/08/php-manual-2-control/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 03:02:03 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP帮助手册]]></category>
		<category><![CDATA[运算符]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=929</guid>
		<description><![CDATA[PHP帮助手册拾遗：流程控制 1、PHP支持用冒号的if语句 1 2 3 4 5 if &#40;expr1&#038; [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>PHP帮助手册拾遗：流程控制</p>
<p><strong>1、PHP支持用冒号的if语句</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>expr1<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>                                                     
    statement1
<span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span>expr2<span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
    statement2
<span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>endif后面有分号</p>
<p><strong>2、PHP 也支持用冒号的 for 循环的替代语法。</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>expr1<span style="color: #339933;">;</span> expr2<span style="color: #339933;">;</span> expr3<span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>                                                
  statement<span style="color: #339933;">;</span>
  <span style="color: #339933;">...</span>
<span style="color: #b1b100;">endfor</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>很python的作法，只是还是需要加上endfor作为结束标记</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endfor</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>3、foreach PHP 5 起，可以很容易地通过在 $value 之前加上 &#038; 来修改数组的单元。此方法将以引用赋值而不是拷贝一个值。</strong><br />
如下所示代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span> <span style="color: #b1b100;">as</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>                                 
    <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//unset($value);    //    释放$value的引用                               </span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$value</span><span style="color: #339933;">++;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>只是在写修改完后，需要释放$value的引用，否则在下次修改此变量时，会改变数组元素的内容，慎重！（某犯过此错误）</p>
<p><strong>4、break 可以接受一个可选的数字参数来决定跳出几重循环。</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>                                     
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
       <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$j</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #b1b100;">break</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//    直接跳转所在的二重循环                                              </span>
       <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">endfor</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">endfor</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>5、注意在 PHP 中 switch 语句被认为是可以使用 continue 的一种循环结构。</strong><br />
<strong>6、continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。</strong><br />
<strong>7、在 switch 语句中条件只求值一次并用来和每个 case 语句比较。在 elseif 语句中条件会再次求值。</strong><br />
case 表达式可以是任何求值为简单类型的表达式，即整型或浮点数以及字符串。不能用数组或对象，除非它们被解除引用成为简单类型。<br />
<strong>8、declare 结构用来设定一段代码的执行指令。declare 的语法和其它流程控制结构相似</strong><br />
directive 部分允许设定 declare 代码段的行为。目前只认识一个指令：ticks</p>
<p>Tick 是一个在 declare 代码段中解释器每执行 N 条低级语句就会发生的事件。N 的值是在 declare 中的 directive 部分用 ticks=N 来指定的。<br />
在每个 tick 中出现的事件是由 register_tick_function() 来指定的。</p>
<p>Ticks 很适合用来做调试，以及实现简单的多任务，后台 I/O 和很多其它任务。<br />
手册上的示例：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> profile<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dump</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span>                                          
<span style="color: #009900;">&#123;</span>
   static <span style="color: #000088;">$profile</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// Return the times stored in profile, then erase it</span>
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$dump</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$profile</span><span style="color: #339933;">;</span>
       <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$profile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000088;">$profile</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set up a tick handler</span>
<span style="color: #990000;">register_tick_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;profile&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Initialize the function before the declare block</span>
profile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Run a block of code, throw a tick every 2nd statement</span>
<span style="color: #000000; font-weight: bold;">declare</span><span style="color: #009900;">&#40;</span>ticks<span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #b1b100;">echo</span> <span style="color: #990000;">similar_text</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">*</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;;&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Display the data stored in the profiler</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>profile <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>上面的这功能真没听说过，作为一个后学末进的程序员，看手册是必须的。</p>
<p><strong>10、return</strong><br />
return() 是语言结构而不是函数，仅在参数包含表达式时才需要用括号将其括起来。当返回一个变量时通常不用括号，也建议不要用，这样可以降低 PHP 的负担。<br />
注: 当用引用返回值时永远不要使用括号，这样行不通。只能通过引用返回变量，而不是语句的结果。如果使用 return ($a); 时其实不是返回一个变量，而是表达式 ($a) 的值（当然，此时该值也正是 $a 的值）。</p>
<p><strong>11、include() 是一个特殊的语言结构，其参数不需要括号。在比较其返回值时要注意。</strong><br />
例如，test.txt中包含字符串 ‘abc’，则判断包含test.txt中是否返回’abc’语句不能以函数的调用方式。<br />
如下所示代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'abc'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>                                            
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'yes'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">/* 以上是错误的代码，程序会执行错误，显示warning */</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'abc'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'yes'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>12、从PHP3.0开始添加了goto语句，但是此语句不能跳转进入loop或switch 语句</strong></p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/08/php-manual-2-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP帮助手册拾遗一：运算符</title>
		<link>https://www.phppan.com/2010/08/php-manual-1-operators/</link>
		<comments>https://www.phppan.com/2010/08/php-manual-1-operators/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 01:34:39 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP帮助手册]]></category>
		<category><![CDATA[运算符]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=915</guid>
		<description><![CDATA[PHP帮助手册拾遗一：运算符 1、基本的赋值运算符是“=”。一开始可能会以为它是“等于”，其实不是的。 它实际 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>PHP帮助手册拾遗一：运算符</p>
<p>1、基本的赋值运算符是“=”。一开始可能会以为它是“等于”，其实不是的。<br />
<strong>它实际上意味着把右边表达式的值赋给左边的运算数。</strong></p>
<p>赋值运算表达式的值也就是所赋的值。也就是说，“$a = 3”的值是 3。这样就可以做一些小技巧：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// $a 现在成了 9，而 $b 成了 4。     </span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>2、取模 $a % $b 在 $a 为负值时的结果也是负值。</strong><br />
<strong>3、 尽管 ! 比 = 的优先级高，PHP 仍旧允许类似如下的表达式：if (!$a = foo())，在此例中 foo() 的输出被赋给了 $a。</strong><br />
同样，如下代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>                  
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> 
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>先赋值，再进行与运算<br />
此问题鸟哥有一篇文章有谈到过，猛击http://www.laruence.com/2010/07/26/1668.html</p>
<p><strong>4、如果比较一个整数和字符串，则字符串会被转换为整数。</strong><br />
如果比较两个数字字符串，则作为整数比较。此规则也适用于 switch 语句。</p>
<p><strong>5、执行运算符</strong><br />
PHP 支持一个执行运算符：反引号（&#8220;）。注意这不是单引号！PHP 将尝试将反引号中的内容作为外壳命令来执行，并将其输出信息返回（例如，可以赋给一个变量而不是简单地丢弃到标准输出）。使用反引号运算符“`”的效果与函数 shell_exec() 相同。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> `ls <span style="color: #339933;">-</span>al`<span style="color: #339933;">;</span>                                           
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>注: 反引号运算符在激活了安全模式或者关闭了 shell_exec() 时是无效的。</p>
<p><strong>6、递增／递减运算符不影响布尔值。递减 NULL 值也没有效果，但是递增 NULL 的结果是 1。</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #339933;">--</span><span style="color: #000088;">$b</span><span style="color: #339933;">;</span>                                                                          
<span style="color: #b1b100;">echo</span> <span style="color: #339933;">++</span><span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #339933;">--</span><span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #339933;">++</span><span style="color: #000088;">$c</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>输出在个1和一个空值</p>
<p><strong>7、+ 运算符把右边的数组附加到左边的数组后面，但是重复的键值不会被覆盖。</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'key1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                                 
<span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$array1</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$array2</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>输出array(3) { [0]=>  int(1) ["key"]=>  int(2) ["key1"]=>  int(4) }<br />
数组2中的3已经不存在了，数组1中的2没有被覆盖</p>
<p><strong>8、使用括号可以增强代码的可读性。</strong></p>
<p><strong>9、除号（“/”）总是返回浮点数，除非这两个运算数是整数（或由字符串转换成的整数）</strong><br />
这里手册中有一点没有说明，返回的值必须是整数才行，否则如 6/4的结果就是float了<br />
//此处在某个中文手册中有翻译错误，意思完全弄反了，纠结&#8230;<br />
代码示例：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                                                                     
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>10、空字符串，false，NULL等值在算术运算符中将会以0对待。</strong><br />
如下所示代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #cc66cc;">5</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br&gt;'</span><span style="color: #339933;">;</span>                                                                
<span style="color: #b1b100;">echo</span> <span style="color: #cc66cc;">5</span> <span style="color: #339933;">+</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #cc66cc;">5</span> <span style="color: #339933;">/</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>11、 加号、减号、点号运行的优先级是一样一样滴</strong><br />
如下所示代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;string &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$var</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>输出3</p>
<p><strong>12、instanceof后面不能接字符串，可以直接接类名或包含类名的变量</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Demo <span style="color: #009900;">&#123;</span>                                                                     
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$demo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Demo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* 以变量的形式存储类名字符串是正确的 */</span>
<span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Demo&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$demo</span> instanceof <span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'yes'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* 不能直接接类名的字符串，即类名不能加双引号 */</span>
<span style="color: #000088;">$demo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Demo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$demo</span> instanceof <span style="color: #0000ff;">&quot;Demo&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'yes'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* 类名上无引号 */</span>
<span style="color: #000088;">$demo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Demo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$demo</span> instanceof Demo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'yes'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/08/php-manual-1-operators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
