Home > Backend Development > PHP Tutorial > 解析用PHP实现var_export的详细介绍_php技巧

解析用PHP实现var_export的详细介绍_php技巧

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-17 09:00:07
Original
925 people have browsed it
复制代码 代码如下:

/**
 * PHP 实现var_export();功能
 */
   $arr = array("1","2","3");
   $arr1 = array('a'=>NULL,'b'=>array('1'=>3));
   var_export($arr);
   //var_export($arr1);
  function varExport($arr){
     $ret = "array( ";
     foreach($arr as $k=>$v){
        $ret .= (is_numeric($k) ? $k : "'".$k."'");
        $ret .= ' => ';
        $_type = strtolower(gettype($v));
        switch($_type){
           case 'integer':
               $ret .= $v." ,";
               break;
           case 'array':
                $ret .= varExport($v).',';
                break;
           case 'null':
                $ret .= "NULL ,";
                break;
           default:
               $ret  .= "'".$v."',";
               break;
        }
     }
     $ret .= " )";
     return $ret;
  }
  //echo varExport($arr);
   echo varExport($arr);
   //bool is_numeric ( mixed $var )如果 var 是数字和数字字符串则返回 TRUE,否则返回 FALSE
   //故不能用 is_numeric 验证是否为int类型。。。
?>
Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template