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

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

WBOY
Release: 2016-06-13 11:47:12
Original
722 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类型。。。
?>

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!