如何重写此代码以提高其清晰度?

eri*_*ric 4 php

你能写这个'清洁'吗?只是一个初学者的简单问题:)

if(isset($_GET['tid']) && trim($_GET['tid'])!==""){
$act = 'tid';
$tid = trim($_GET['tid']);

}elseif(isset($_GET['fid']) && trim($_GET['fid'])!==""){
$act = 'fid';
$fid = trim($_GET['fid']);

}elseif(isset($_GET['mid']) && trim($_GET['mid'])!==""){
$act = 'mid';

}elseif(isset($_GET['act']) && trim($_GET['act'])!==""){
$act = trim($_GET['act']);

}else{
$act = "";
}
Run Code Online (Sandbox Code Playgroud)

pok*_*oke 5

我会这样做:

$tid = isset( $_GET['tid'] ) ? trim( $_GET['tid'] ) : '';
$fid = isset( $_GET['fid'] ) ? trim( $_GET['fid'] ) : '';
$mid = isset( $_GET['mid'] ) ? trim( $_GET['mid'] ) : '';
$act = isset( $_GET['act'] ) ? trim( $_GET['act'] ) : '';

if ( empty( $act ) ) // act not set, construct the act from the other GET vars
{
    if ( !empty( $tid ) )
        $act = 'tid';
    else if ( !empty( $fid ) )
        $act = 'fid';
    else if ( !empty( $mid ) )
        $act = 'mid';
}
Run Code Online (Sandbox Code Playgroud)

编辑:当然你可以让它更短,但问题是如何编写"提高其清晰度".我理解清晰度是一种让它更容易理解的东西,代码的一部分会发生什么.我认为原始代码背后的实际逻辑在我的解决方案中非常明确.