本篇文章主要介绍php通用返回值的设置方法详解,感兴趣的朋友参考下,希望对大家有所帮助。
遇到一个不错的php代码。记录一下。
在写php代码时,经常会遇到需要返回值的情况,可以统一设置一下返回值的格式。
下面就是一个不错的例子。
配置类return.conf.php
<?php
define("return_val", "return array('code' => 0, 'msg' => '', 'data' => '');");
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 '{print $1}'";
exec($cmd, $output, $code);
if (return_success !== $code){
$result['code'] = return_runtime_err;
$result['msg'] = "exec $cmd err";
return $result;
}
$result['data'] = $output[0];
return $result;
}
print_r(get_file_line("test.php"));
?>
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
dvwa之php+mysql手工注入
php5.5.12 下调试 soap 报错信息
php中 htmlpurifier防xss攻击
以上就是php通用返回值的设置方法详解的详细内容。