如何获取已在PHP函数内声明的变量的值并将其返回到外部

时间:2015-05-07 13:15:25

标签: php function variables scope

function count($data) {
    $json = file_get_contents( 'http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=' . $data . '&pretty=1' );
    $json_data = json_decode($json, true);
    $count = $json_data[0]['total_count'];
    if(empty ($count) ) {
        return 'n/a';
    } else {
        if( $count < 1000 ) return $count;
        $x = round($count);
        $x_number_format = number_format($x);
        $x_array = explode(',', $x_number_format);
        $x_parts = array('k', 'm', 'b', 't');
        $x_count_parts = count($x_array) - 1;
        $x_display = $x;
        $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
        $x_display .= $x_parts[$x_count_parts - 1];
        return  $x_display;
    }

}

如何在上述函数之外仅获取$count的值?调用上面的函数肯定会返回$x_display,我想在代码中的其他地方回显简单计数$count

2 个答案:

答案 0 :(得分:3)

使用$x_display返回。通常,这是以数组形式完成的,您可以使用list()来获取每个部分:

function count($data) {
    $json = file_get_contents( 'http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=' . $data . '&pretty=1' );
    $json_data = json_decode($json, true);
    $count = $json_data[0]['total_count'];
    if(empty ($count) ) {
        return 'n/a';
    } else {
        if( $count < 1000 ) return $count;
        $x = round($count);
        $x_number_format = number_format($x);
        $x_array = explode(',', $x_number_format);
        $x_parts = array('k', 'm', 'b', 't');
        $x_count_parts = count($x_array) - 1;
        $x_display = $x;
        $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
        $x_display .= $x_parts[$x_count_parts - 1];
        return  array($x_display, $count);
    }

}

list($x_display, $count) = count($data);

虽然返回两个值通常不是一个好习惯。把它分解成两个函数我可能是更好的方法:

function count($data) {
    $json = file_get_contents( 'http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=' . $data . '&pretty=1' );
    $json_data = json_decode($json, true);
    return $json_data[0]['total_count'];
}

function getDisplay($count) {
    if(empty ($count) ) {
        return 'n/a';
    } else {
        if( $count < 1000 ) return $count;
        $x = round($count);
        $x_number_format = number_format($x);
        $x_array = explode(',', $x_number_format);
        $x_parts = array('k', 'm', 'b', 't');
        $x_count_parts = count($x_array) - 1;
        $x_display = $x;
        $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
        $x_display .= $x_parts[$x_count_parts - 1];
        return  $x_display;
    }
}

$count = count($data);
$x_display = getDisplay($count);

使用某人不可避免地建议的global关键字。这是一种糟糕的编程实践,可能导致难以发现错误。

答案 1 :(得分:1)

在我看来,这是一个完美的课程用途。

class Classname {
  protected $count;

  function getXDisplay() {
    $json = file_get_contents( 'http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=' . $data . '&pretty=1' );
    $json_data = json_decode($json, true);
    $this->count = $count = $json_data[0]['total_count'];
    if(empty ($count) ) {
       return 'n/a'; // should be null ?
    } else {
       if( $count < 1000 ) return $count;
       $x = round($count);
       $x_number_format = number_format($x);
       $x_array = explode(',', $x_number_format);
       $x_parts = array('k', 'm', 'b', 't');
       $x_count_parts = count($x_array) - 1;
       $x_display = $x;
       $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
       $x_display .= $x_parts[$x_count_parts - 1];
       return  $x_display;
    }
  }

  function getCount() {
    if (is_null($this->count)) $this->getXDisplay();
    return $this->count;
  }
}

不要使用凌乱的返回值,只需调用与返回值相关的类方法。