Home php教程 php手册 var_export 与 var_dump用法介绍

var_export 与 var_dump用法介绍

May 25, 2016 pm 04:47 PM
var_dump

下面我们来介绍一下关于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

 


文章链接:

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)