function.var-export.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 245 行
HTML
245 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Outputs or returns a parsable string representation of a variable</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ref.var.html">Variable handling Functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="refs.webservice.html">Web Services</a></div> <div class="up"><a href="ref.var.html">Variable handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.var-export" class="refentry"> <div class="refnamediv"> <h1 class="refname">var_export</h1> <p class="verinfo">(PHP 4 >= 4.2.0, PHP 5)</p><p class="refpurpose"><span class="refname">var_export</span> — <span class="dc-title">Outputs or returns a parsable string representation of a variable</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><b><b>var_export</b></b></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$expression</tt></span> [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$return</tt></span> ] )</div> <p class="simpara"> <b>var_export()</b> gets structured information about the given variable. It is similar to <a href="function.var-dump.html" class="function">var_dump()</a> with one exception: the returned representation is valid PHP code. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">expression</tt></i></span> <dd> <p class="para"> The variable you want to export. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">return</tt></i></span> <dd> <p class="para"> If used and set to <b><tt>TRUE</tt></b>, <b>var_export()</b> will return the variable representation instead of outputing it. </p> <blockquote><p><b class="note">Note</b>: This function uses internal output bufferingwith this parameter so it can not be used inside an<a href="function.ob-start.html" class="function">ob_start()</a> callback function.<br /></p></blockquote> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the variable representation when the <i><tt class="parameter">return</tt></i> parameter is used and evaluates to <b><tt>TRUE</tt></b>. Otherwise, this function will return <b><tt>NULL</tt></b>. </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.1.0</td> <td colspan="1" rowspan="1" align="left"> Possibility to export classes and arrays containing classes using the <a href="language.oop5.magic.html#language.oop5.magic.set-state" class="link">__set_state</a> magic method. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>var_export()</b> Examples</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= array (</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, array (</span><span style="color: #DD0000">"a"</span><span style="color: #007700">, </span><span style="color: #DD0000">"b"</span><span style="color: #007700">, </span><span style="color: #DD0000">"c"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>array ( 0 => 1, 1 => 2, 2 => array ( 0 => 'a', 1 => 'b', 2 => 'c', ),)</pre></div> </pre></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$b </span><span style="color: #007700">= </span><span style="color: #0000BB">3.1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$v </span><span style="color: #007700">= </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$v</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>3.1</pre></div> </pre></div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #2 Exporting classes since PHP 5.1.0</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{ public </span><span style="color: #0000BB">$var</span><span style="color: #007700">; }<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">-></span><span style="color: #0000BB">var </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>A::__set_state(array( 'var' => 5,))</pre></div> </pre></div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #3 Using <a href="language.oop5.magic.html#language.oop5.magic.set-state" class="link">__set_state</a> (since PHP 5.1.0)</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A<br /></span><span style="color: #007700">{<br /> public </span><span style="color: #0000BB">$var1</span><span style="color: #007700">;<br /> public </span><span style="color: #0000BB">$var2</span><span style="color: #007700">;<br /><br /> public static function </span><span style="color: #0000BB">__set_state</span><span style="color: #007700">(</span><span style="color: #0000BB">$an_array</span><span style="color: #007700">)<br /> {<br /> </span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-></span><span style="color: #0000BB">var1 </span><span style="color: #007700">= </span><span style="color: #0000BB">$an_array</span><span style="color: #007700">[</span><span style="color: #DD0000">'var1'</span><span style="color: #007700">];<br /> </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-></span><span style="color: #0000BB">var2 </span><span style="color: #007700">= </span><span style="color: #0000BB">$an_array</span><span style="color: #007700">[</span><span style="color: #DD0000">'var2'</span><span style="color: #007700">];<br /> return </span><span style="color: #0000BB">$obj</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">-></span><span style="color: #0000BB">var1 </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">-></span><span style="color: #0000BB">var2 </span><span style="color: #007700">= </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;<br /><br />eval(</span><span style="color: #DD0000">'$b = ' </span><span style="color: #007700">. </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">) . </span><span style="color: #DD0000">';'</span><span style="color: #007700">); </span><span style="color: #FF8000">// $b = A::__set_state(array(<br /> // 'var1' => 5,<br /> // 'var2' => 'foo',<br /> // ));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>object(A)#2 (2) { ["var1"]=> int(5) ["var2"]=> string(3) "foo"}</pre></div> </pre></div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: Variables of type <a href="language.types.resource.html" class="type resource">resource</a> couldn't be exported by this function. <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: <b>var_export()</b> does not handle circular references as it would be close to impossible to generate parsable PHP code for that. If you want to do something with the full representation of an array or object, use <a href="function.serialize.html" class="function">serialize()</a>. <br /> </p></blockquote> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.print-r.html" class="function" rel="rdfs-seeAlso">print_r()</a></li> <li class="member"><a href="function.serialize.html" class="function" rel="rdfs-seeAlso">serialize()</a></li> <li class="member"><a href="function.var-dump.html" class="function" rel="rdfs-seeAlso">var_dump()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ref.var.html">Variable handling Functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="refs.webservice.html">Web Services</a></div> <div class="up"><a href="ref.var.html">Variable handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?