Home  >  Article  >  Backend Development  >  Detailed explanation of setting method of PHP general return value

Detailed explanation of setting method of PHP general return value

墨辰丷
墨辰丷Original
2018-05-24 11:30:581719browse

本篇文章主要介绍PHP通用返回值的设置方法详解,感兴趣的朋友参考下,希望对大家有所帮助。

遇到一个不错的php代码。记录一下。

在写php代码时,经常会遇到需要返回值的情况,可以统一设置一下返回值的格式。

下面就是一个不错的例子。

配置类Return.conf.php

<?php
define("return_val", "return array(&#39;code&#39; => 0, &#39;msg&#39; => &#39;&#39;, &#39;data&#39; => &#39;&#39;);");

define("RETURN_SUCCESS",    0); 
define("RETURN_RUNTIME_ERR",  1); 
define("RETURN_FILE_NOT_EXIST", 2); 

class ReturnConf{
  public static function CommonReturn(){
    return eval(return_val);
  }  
}
?>

测试、使用test.php

<?php
require_once("Return.conf.php");

function get_file_line($filename){
  $result = ReturnConf::CommonReturn();
  
  $cmd = "wc -l $filename | awk &#39;{print $1}&#39;";
  exec($cmd, $output, $code);
  if (RETURN_SUCCESS !== $code){
    $result[&#39;code&#39;] = RETURN_RUNTIME_ERR;
    $result[&#39;msg&#39;] = "exec $cmd err";
    return $result; 
  }  

  $result[&#39;data&#39;] = $output[0];
  return $result;
}

print_r(get_file_line("test.php"));
?>

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

DVWA之php+mysql手工注入

php5.5.12 下调试 SOAP 报错信息 

PHP中 HTMLPurifier防XSS攻击

The above is the detailed content of Detailed explanation of setting method of PHP general return value. 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