Home > php教程 > php手册 > body text

var_export 与 var_dump用法介绍

WBOY
Release: 2016-05-25 16:47:25
Original
1056 people have browsed it

下面我们来介绍一下关于var_export 与 var_dump用法,有需要的朋友可参考.var_export必须返回合法的php代码, 也就是说,var_export返回的代码,可以直接当作php代码赋值个一个变量. 而这个变量就会取得和被var_export一样的类型的值

但是, 当变量类型为resource的时候, 是无法简单copy复制的,所以, 当var_export的变量是resource类型时, var_export会返回NULL

实例代码如下:

$res = yblog_mspconfiginit("ratings"); 
var_dump($res); 
var_export($res);结果: 
resource(1) of type (yahoo_yblog)
Copy after login

NULL再比如:

$res = fopen('status.html', 'r'); 
var_dump($res); 
var_export($res);结果: 
resource(2) of type (stream) 
NULL
Copy after login

实例代码如下:

<?php
//php var_export读写实例类
class user {
    var $filepath;
    function __() {
        $this->filepath = "d:/www.phprm.com/group/";
    }
    function cache() {
        $array = $this->db->select(&#39;select group_id,group_name from group&#39;, &#39;hashmap&#39;);
        $fp = fopen($this->filepath, &#39;w&#39;);
        fputs($fp, &#39;<?php return &#39; . var_export($array, true) . &#39;;&#39;);
        fclose($fp);
    }
    function getVar_export($value) {
        $array = require ($this->filepath);
        foreach ($array as $key => $v) {
            if ($key == $value) {
                $selected = &#39; current option&#39;;
            } else {
                $selected = &#39;&#39;;
            }
            $html.= &#39;<option value="&#39; . $key . &#39;"&#39; . $selected . &#39;>&#39; . $v . &#39;</option>&#39;;
        }
        return $html;
    }
}
//使用实例方法
$g = new user();
if (intval($_GET[&#39;iscreate&#39;])) {
    $g->cache();
} else {
    $g->getVar_export(&#39;vv&#39;);
}
//本站原创www.phprm.com转载注明来源
var_dump函数var_dump  (PHP3 >= 3.0.5, PHP4, PHP5)   var_dump--打印变量的相关信息voidvar_dump(mixedexpression[, mixedexpression[, . . . ]])
?> 
Copy after login

此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值.数组将递归展开值,通过缩进显示其结构.   

提示: 为了防止程序直接将结果输出到浏览器,可以使用输出控制函数(output-control functions)来捕获此函数的输出,并把它们保存到一个例如 string 类型的变量中.   

可以比较一下 var_dump() 与 print_r().

实例代码如下:  

<pre class="brush:php;toolbar:false"> 
  <?php
   $a = array(
    1,
    2,
    array(
        "a",
        "b",
        "c"
    )
);
  var_dump($a);
  
/* 输出:
  array(3) { 
  [0]=> 
  int(1) 
  [1]=> 
  int(2) 
  [2]=> 
  array(3) { 
  [0]=> 
  string(1) "a" 
  [1]=> 
  string(1) "b" 
  [2]=> 
  string(1) "c" 
  } 
  } 
  */
   $b = 3.1;
   $c = TRUE;
  var_dump($b, $c);
  
/* 输出:
  float(3.1) 
  bool(true) 
  */
?>
Copy after login

 


文章链接:

随便收藏,请保留本文地址!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!