Sample code sharing about PHP universal return value setting method

黄舟
Release: 2023-03-07 08:34:01
Original
1088 people have browsed it

下面小编就为大家带来一篇关于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);
  }  
}
?>
Copy after login

测试、使用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"));
?>
Copy after login

The above is the detailed content of Sample code sharing about PHP universal return value setting method. 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