search
Homephp教程php手册var_export 与 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)

NULL再比如: 

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

实例代码如下:

<?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[, . . . ]])
?> 

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

提示: 为了防止程序直接将结果输出到浏览器,可以使用输出控制函数(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) 
  */
?>

 


文章链接:

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

Statement
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.