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

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

WBOY
WBOYOriginal
2016-06-01 12:06:15895browse
复制代码 代码如下:
/**
 * 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类型。。。
?>
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