Home  >  Article  >  Backend Development  >  Detailed example of implementing var_export using PHP

Detailed example of implementing var_export using PHP

怪我咯
怪我咯Original
2017-07-06 10:22:001278browse

This article is a detailed analysis and introduction to the method of using PHP to implement var_export. Friends in need can refer to it

The code is as follows:

<?php
/**
 * PHP 实现var_export();功能
 */
   $arr = array("1","2","3");
   $arr1 = array(&#39;a&#39;=>NULL,&#39;b&#39;=>array(&#39;1&#39;=>3));
   var_export($arr);
   //var_export($arr1);
  function varExport($arr){
     $ret = "array( ";
     foreach($arr as $k=>$v){
        $ret .= (is_numeric($k) ? $k : "&#39;".$k."&#39;");
        $ret .= &#39; => &#39;;
        $_type = strtolower(gettype($v));
        switch($_type){
           case &#39;integer&#39;:
               $ret .= $v." ,";
               break;
           case &#39;array&#39;:
                $ret .= varExport($v).&#39;,&#39;;
                break;
           case &#39;null&#39;:
                $ret .= "NULL ,";
                break;
           default:
               $ret  .= "&#39;".$v."&#39;,";
               break;
        }
     }
     $ret .= " )";
     return $ret;
  }
  //echo varExport($arr);
   echo varExport($arr);
   //bool is_numeric ( mixed $var )如果 var 是数字和数字字符串则返回 TRUE,否则返回 FALSE 
   //故不能用 is_numeric 验证是否为int类型。。。
?>

The above is the detailed content of Detailed example of implementing var_export using PHP. For more information, please follow other related articles on the PHP Chinese website!

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