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

Detailed example of implementing var_export using PHP

怪我咯
Release: 2023-03-12 20:34:01
Original
1384 people have browsed it

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类型。。。
?>
Copy after login

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!

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template